In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use ASP.Net 3.5 for Ajax and Web service development, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Here I also introduce how the namespace System.Web.Script determines how the client invokes the Web service method.
Calling Web service method in AJAX can improve the experience of Web users. Microsoft AJAX in ASP.NET3.5 adds its new function, which can call Web service method from client JavaScript without refreshing the whole page. AJAX technology enables you to call server-side methods without post back. The client script can make the Web method of the request, and the method and data can be sent back to the client browser from the server through the data as input parameters.
In order for your application to call ASP.NET Web service using client script, the server asynchronous communication layer will automatically generate JavaScript proxy classes. The proxy class becomes an element of each Web service that is included in the control page.
This is the download of the proxy class of the browser at web page loading time, and provides a client object, proxy invocation method of the Web service. The proxy class of the JavaScript generated by calling the corresponding method. The proxy class opens communication and network services. These requests communicate asynchronously through the browser of the XMLHTTP object.
As shown in the following figure, the client and server communication frameworks on different layers are specified in detail.
Element specifies to register a JavaScript file to be used in the web page. You can make calls on methods only if you register the CallWebServiceMethod.js file, and the script that invokes the Web service method is asynchronous. To get a return value or a request to determine when to return, you must provide a successful callback function. When the callback function is called, the request completes successfully, and the return value, if any, it contains is called from the Web method. You can also provide a failed callback function to handle errors. In addition, you can also use the callback function through the user's background information.
As shown in the following figure, it is the sequence diagram of WCF and Ajax calling Web service.
As mentioned in the previous article (JSON extension applications based on ASP.NET 3.5 Web Service), JSON-JavaScript object symbols are the default serialization format that is used for client-server requests between data transformations. You can disable all currently enabled protocols like HTTP-GET, HTTP-POST, and even the earlier forms of Web services used in SOAP in XML format. The following settings are also used in Web.config files.
Request a Web service method through these levels. You can see how to use a method that requires that it be in an available proxy object and Web request and run by a XMLHttp object on the client browser. On the server side, your request is, as usual, to be XML/JSON serialized by a HTTP handler.
As shown in the following figure, asp.net 3.5 invokes the class diagram of Ajax and Web services.
Using the Web service method in AJAX consists of two steps: the * step is to create and define a Web service. The second step is to use the client script to call the service from a web page. Create a Web service:
In the System.Web.Scripts.Services namespace, you may find an attribute class "ScriptSrvice", which needs to apply to Web service classes so that Web service methods can invoke scripts from the client. This causes the agent to generate a script to generate a proxy object that corresponds to the Web service class.
Similarly, in the same namespace, you may find another property class "ScriptMethod". If you use this property as the Web method, you can specify which HTTP verbs are used to call a method and response form.
The three parameters for this property are described as follows:
UseHttpGet: if set to true, this method will be called to use the HTTP GET command. The default is false.
ResponseFormat: specifies whether to respond to the simple JSON or XML that will be serialized. The default is JSON.
XmlSerializeString: specifies whether all return types, including string types, are JSON serialized by values serialized for XML, ignoring XmlSerializeString's continuous responses.
Now, create a new Web using the ASP.NET Web Service template in Microsoft Visual Studio 2008 and modify the Web service class as follows:
Using System.Web.Script.Services;namespace AjaxWebService {[WebService (Namespace = "http://localhost:1382/AjaxWebService/")] [WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)] [ScriptService] public class Service: System.Web.Services.WebService {string myXmlData = @" / This method uses JSON response formatting / / [ScriptMethod (ResponseFormat = ResponseFormat.Json)] [WebMethod] public string getNextBackupDate (int months) {return DateTime.Now.AddMonths (months) .ToShortDateString () } / This method uses XML response formatting / [ScriptMethod (ResponseFormat = ResponseFormat.Xml)] [WebMethod] public XmlDocument GetBookTitle () {XmlDocument xmlDoc = new XmlDocument (); xmlDoc.LoadXml (myXmlData); return xmlDoc } / This method uses HTTP-GET protocol to call it / [ScriptMethod (UseHttpGet = true)] [WebMethod] public string HelloWorld () {return "Hello, world";}
Note: the ScriptService usage created by the Web service will not be defaulted by the browser as above. You need to modify the settings in the file Web.config file as follows to test the above Web service.
Calling the Web service method uses client script, and the Asp.Net Web service method does not return asynchronously from the client script and does not refresh the entire page. There is only the browser of the server and client that transfers data between them.
Currently,. NET 3.5 framework supports Web services and client pages can be in the same domain (the same site).
Now add a new "Ajax activate Web page" to write a JavaScript function to call the Web service and callback methods with the existing Web service project and the tag specified in the web page that adds the control. The Web service method is called by using the proxy class and parameter list, the function name is successfully called, the failed callback function is called, and the user is called through the requirements of additional parameters.
Function CallNextDate () {AjaxWebService.Service.getNextBackupDate (1, OnSucceeded);} function CallHelloWorld () {AjaxWebService.Service.HelloWorld (OnSucceeded);} function CallBookTitle () {AjaxWebService.Service.GetBookTitle (OnSuccess, OnFail, "XmlDocument");} / / This is the callback function that processes the WebService return value in JSON format. Function OnSucceeded (result) {var myresult = document.getElementById ("Text1"); myresult.value = result;} / / This is the callback function that processes the Web Service return value in XML format. Function OnSuccess (result) {var myresult = document.getElementById ("Text1"); myresult.value = "Title:" + result.documentElement.text;} / / This is the callback function that processes the Web Service return value in XML format. Function OnFail (error) {var myresult = document.getElementById ("Text1"); myresult.value = "Service Error:" + error.get_message ();} # Text1 {width: 375px;} # Button2 {width: 140px;}
Result:
In the tag above, the path attribute of the notification is at the ScriptManager control point of the ServiceReference element to the Web service class. This makes the Web service method called the default.aspx page from the script.
Embedded functions CallNextDate, CallHelloWorld, and CallBookTitle are the three Web service methods used to invoke. The OnSuccess and OnFail methods are callback methods, and the methods that get the executed Web service are executed. In order for the client's Web page to work properly, you need to add the Web.config file for the following settings.
Use DLL such as System.Web.Extensions.dll referenced by Microsoft ASP.NET3.5 and take advantage of the AJAX technology in the built-in ASP.NET3.5.
After reading the above, do you have any further understanding of how to use ASP.Net 3.5 for Ajax and Web service development? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 246
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.