Ajax Study Notes 2-client access to WebService (I)

Source: Internet
Author: User

1. Access webservie (. asmx) Basics through JS
1. After referencing the webservie proxy, you can find a JS script starting with type. registernamespace ('namespace ') in the output HTML, that is, the client proxy generated by scriptmanager.

Code segment.
2. the attribute inlinescript = "true" of ASP: servicereference indicates whether the proxy is cached on the page (in HTML source code)
3. In JS functions, arguments indicates the parameter set object of the current function; arguments. Length indicates the total number of parameters.
Ii. js access to pagemethod (. aspx) Basics
1. pagemethod method server-side limitation:-defined only on the ASPX page-only public static methods-marked by webmethod attribute

-The enablepagemethod attribute of scriptmanager is true, and the pagemethod proxy is generated.
<Asp: scriptmanager id = "scriptmanager1" runat = "server" enablepagemethods = "true"/>
Client call: pagemethods. Method Name
TIPS: the date type value transmitted from the server to the client is actually transmitted (the number of milliseconds from the start date to the present and the time zone information). When tostring () is executed on the client, the time zone is automatically converted.
Ii. Ajax error handling
1. An additional error callback function can be provided during the call. errors are not handled by default.
Errors: including errors and exceptions thrown by the server
2. the timeout attribute can only be at the WebService (pagemethods) level.
3. Ajax client error handling class SYS. net. webserviceerror
Attribute: timedout-whether to time out; message-error message; exceptiontype server exception type; stacktrace-Method for exception generation
4. Ajax client call WebService error handling
Function getdivision (a, B )//
{
Errorhandling. set_timeout (2000); // set the timeout value.
Errorhandling. getdivision (a, B, null, failedcallback); // The fourth parameter is the error callback function.
}

Function failedcallback (error) // error callback function
{
VaR message = string. Format (
"Timeout: {0} \ nmessage: {1} \ nexceptiontype: {2} \ nstacktrace: {3 }",
Error. get_timedout (),
Error. get_message (),
Error. get_exceptiontype (),
Error. get_stacktrace ());
III. Basic use of complex data types
1. Public attributes and fields can be called
Note: In the window object, the object implementing the ilist interface corresponds to the JS array; when implementing the idictionary interface, the key must be a string
Alert (Message );
}
For example, transfer the dictionary type
Server:
[Webmethod]
Public idictionary <string, employee> getemployees ()
{
Dictionary <string, employee> result = new dictionary <string, employee> ();

Employee emp1 = new employee ();
Emp1.firstname = "Jeffrey ";
Emp1.lastname = "Zhao ";
Emp1.salary = 1000;
Result [emp1.fullname] = emp1;

Employee emp2 = new employee ();
Emp2.firstname = "Tom ";
Emp2.lastname = "Chen ";
Emp2.salary = 2000;
Result [emp2.fullname] = emp2;
Return result;
}
Client:
Function getemployees ()
{
Complextype. getemployees (getemployeessucceeded );
}
Function getemployeessucceeded (result)
{
For (VAR name in result)
{
Alert (name + ":" + result [name]. Salary)
}
}
Iv. client proxy Usage Details
1. function call complete signature invoke (arg1 [,... argn] [, callback function upon success] [, callback function upon error,] [, object to be transferred between the invoke function and callback function]);
2. Name of the callback function when the complete signature of the callback function is successful (return result [, object to be transferred between the invoke function and the callback function] [, called method name]);
Name of the callback function upon failure (return result [, object to be passed between the invoke function and the callback function] [, called method name]);
3. Default WebService-level attributes
Timeout: the number of milliseconds that have timed out;
Defaultusercontext: the object to be passed between the invoke function and the callback function;
The name of the callback function when defasecsecceededcallback is successful;
The name of the callback function when the ultfailedcallback error occurs.
If the above default value is specified and invoke does not write these parameters, the function is called according to the default value.

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.