cross-domain ajax request to call webservice interfaces

1.WebService write interfaces

Step: Create a new web project = "Add web service =" write the interface method = "then release (local testing can be directly put this web service up and running).

How to make the key external Ajax call.

First, configure WebService project configuration file (web.config) red part must be configured so that third parties can call the interface method (Tested by directly pasted on ok), can not understand Baidu.

 

<configuration>
    <system.web>
      <webServices>
        <protocols>
          <add name="HttpSoap"/>
          <add name="HttpPost"/>
          <add name="HttpGet"/>
          <add name="Documentation"/>
        </protocols>
      </webServices>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="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>
    </httpProtocol>
  </system.webServer>
</configuration>

2, secondly, posted here WebService code section, and here I return to a Person a custom collection GetPersonList (), for Ajax calls.

You need to configure when (1) release [WebService (Namespace = "http://192.168.1.90:5555/")]// custom domain address after you publish here. Of course, you can use localhost local test or use the default can be.

(2) to release the [System.Web.Script.Services.ScriptService] notes.

These two steps do write interface release WebService, access http://192.168.1.90:5555/XXX.asmx address.

the System the using; 
the using the System.Collections.Generic; 
the using the System.Linq; 
the using the System.Web; 
the using the System.Web.Services; 

namespace the MyWebService 
{ 
    /// < Summary > 
    /// summary of the MyWebService 
    /// </ Summary > 
    [WebService (the Namespace = "HTTP: // localhost: 47737 /")] 
    [WebServiceBinding (conformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem (false)] 
    // To allow the use of ASP.NET AJAX from a script calling this Web service, uncomment the following line. 
    [System.Web.Script.Services.ScriptService] 
    public class the MyWebService: the System.Web.Services.WebService 
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int Add(int num1,int num2)
        {
            return num1 + num2;
        }
    }
}

3. Third-party Ajax call.

$.ajax({
                url: "http://localhost:47737/MyWebService.asmx/Add",
                type: "post",
                data: "{ 'num1': 2,'num2': 5 }",
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    alert(data.d);
                },
                error: function () {
                    alert("发送ajax请求失败!");
                }
            });

postscript:

 1) ajax request header is provided through content-type: 'application / json ' will appear like a string return json, the get request corresponding to this error webservice, after removing the content-type get / post request can be performed correctly, but it returns xml, not json.

2) If get request must be webservice, ScriptMethod need to specify the attribute UseHttpGet = true, get web.config configured / post for access should only return xml instead provided over the content-type: 'application / json ' Back json, so get requests to be wrong.

Guess you like

Origin www.cnblogs.com/superfeeling/p/11600854.html