WebService code
1 /// <summary>2 ///Summary description of Messervice3 /// </summary>4[WebService (Namespace ="http://tempuri.org/")]5[WebServiceBinding (ConformsTo =wsiprofiles.basicprofile1_1)]6 //[System.ComponentModel.ToolboxItem (false)]7 //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. 8 [System.Web.Script.Services.ScriptService]9 Public classMESService:System.Web.Services.WebServiceTen { One A[WebMethod (Description ="")] - Public stringGETDATARETURNSTR (stringstrxml) - { the //your results to deal with - #regionWorking with Data - - #endregion + returnCommon.JsonHelper.SerializeToJson (NewStudent () {id ="1", name ="Test" }); - } + A at } - [Serializable] - Public classStudent - { - Public stringID {Get;Set; } - Public stringName {Get;Set; } in}
Information to add in the WebService file
[System.Web.Script.Services.ScriptService]
Need to add nodes in the Webconfig file
1. Resolving cross-domain access issues
<system.webServer> <!--resolving cross-domain requests--"Access-control-allow-methods"Value="Options,post,get"/> <add name="access-control-allow-headers"Value="X-requested-with,content-type"/> <add name="Access-control-allow-origin"Value="*"/> </customHeaders> 2. Solve the problem that the local call succeeds and the external network call fails.
<system.web> <webServices> <protocols> <add name="httpsoap "/> <add name="httppost"/> <add name=" HttpGet"/> <add name="documentation"/> </protocols> </webServices></system.web>
Ajax Code
1 $.ajax ({2 Async:true,3 type: "Post",4 URL: "Http://localhost:41453/IDataServer/MESService.asmx/GetDataReturnStr",5 data: "{strxml: ' 123123123 '}",6 dataType: "JSON",7 contentType: "Application/json; Charset=utf-8 ",8 success:function (JSON) {9 var dataobj = eval ("(" + JSON.D + ")");Ten alert (dataobj.id + ' \ r \ n ' + dataobj.name); One }, A error:function () { - - }, the complete:function () { - } -});
This enables AJAX access to the WebService interface.
Ajax calls WebService resolve cross-domain and post-to-server local calls successful external network failure issues