Ajax-basic usage of client access to webservice

Source: Internet
Author: User

Before learning aps.net, I learned how to use the server to access webservice. At that time, I implemented an example: web server simulates online shopping, today I learned how to access webserivice directly from the client when I learned asp.net ajax. This direct access to webserver by the client reflects ajax's idea of Asynchronously refreshing data.

Client Access to webservice Basics
To create a webservice, follow these steps:

1. Create an aps.net webservice

2. Add the tag [scriptService]

3. Write the webservice method and add the [webMethod] flag to release the server method.

Steps for accessing webservice from a client

1. Introduce the asmx file in the inserted scrptmanager.

2. input parameters

3. Write a method as a successful callback function.

4. You can also write an error handling function as a failed callback function.

Below is a small example written by myself: It is generally necessary to input two numbers on the client to call the division method on the webservic side. If the running result is successfully output, if the input or output content is incorrect. (Very simple)

Webservic end

Public class divsion: System. web. services. webService {[WebMethod] public int getDivison (int a, int B) // division operation {return a/B;} [WebMethod] public int timeOut () {Thread. sleep (5000); // set the Sleep time to return 0 ;}}

Client

        Divided    

Running result

When two integers are entered:


When the divisor is 0, an error is thrown.

Vcq90 + signature/HIodKzw + a1xL7fzOW/2Lz + signature + CjxoMj6/zbuntsu3w87Kt/Signature + signature + authorization + authorization + CjxwPs/Cw + authorization/7O8cb3tsvQtMjru/HIobWxx7DKsbzktcS3vbeoo6y/authorization + t/7O8cb3tss8L3A + authorization = "brush: java; "> [WebMethod] public static DateTime GetCurrentTime () {// return DateTime is displayed based on the time of the client. utcNow ;}

Client

/head>    

Client proxy Usage Details
Complete function call Signature

Invoke (arg1,... argn, onSuccessed, onFailed, userContext)

Arg1: Parameters

OnSuccessed: successfully called Function

OnFailed: the function that fails to be called, or the function that handles the error.

UserContext: Call a method and accept the method to continue processing. It is a different method, so passing parameters can be placed in usercontext.

Complete callback function Signature

OnSuccessed/onFailed (result, UserContext, methodName)

Default WebService-level attributes
Timeout: Timeout
Defaultusercontext: Default usercontext

Defaultsuccessededcallback: the default success callback function.

Defaultfailedcallback: default error handling method

Summary:

This part is just getting started and learning a little slow, but I am still very interested in this part, because asynchronous transmission indeed improves the efficiency. Of course, the learning of accessing webserivice through this part of the client is becoming more and more powerful as the asp.net ajax framework.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.