Ajax calls WebService resolve cross-domain and post-to-server local calls successful external network failure issues

Source: Internet
Author: User

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

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.