使用jQuery jsonp跨域调用ASP.NET Web服务

时间:2015-12-02 10:07:48

标签: jquery ajax web-services cors jsonp

我尝试了很多例子,但仍然坚持。

我正在尝试使用Ajax Jquery调用跨域ws调用但返回以下错误:

我的Ajax调用是:

jQuery.support.cors = true;
        var para = JSON.stringify({ orgname: var0, ccGuid: var1 , accountTypeGuid: var2 });

   $.ajax({
        type: "GET",
        url:http://example:5052/WebServices/CustomerView.asmx/GetCustomerPlans,
        async: true,
        contentType: "application/json; charset=utf-8",
        data: para,
        dataType: "jsonp",
        crossDomain: true,
        xhrFields: {
            'withCredentials': true
       },
        success: UpdateCustomerViews,
        error: fail
    });

传递的参数是:

{"orgname":"abc","ccGuid":"4194716f-a068-e411-80c1-0050568c48b6","accountTypeGuid":"a53cd1ca-716a-e411-80c2-0050568c48b6"}

url在fiddler中显示:

http://example:5052/WebServices/CustomerView.asmx/GetCustomerPlans?callback=jQuery110203820646793817735_1448975800168&  {"orgname":"abc","ccGuid":"4194716f-a068-e411-80c1-0050568c48b6","accountTypeGuid":"8d89ffdf-91d7-e411-80d0-0050568c48b6"}&_=1448975800169

我的webMethod:

 [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public string GetCustomerPlans(string orgname, string ccGuid, string accountTypeGuid)
    {
           //some Buisness logic
           JavaScriptSerializer javascipt = new JavaScriptSerializer();
           string result = javascipt.Serialize(CCIDs.ToArray());
           return result;           
    }

Fiddler中显示的错误消息:

  

System.InvalidOperationException:缺少参数:orgname。在   System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection中   收藏)   System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
  在   System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

请建议我犯错的地方。

仅供参考:原始工作代码如下所示,它运行良好,但在Chrome和Mozila中失败了,我开始了解跨源策略问题,然后我决定更改JSONP的相同代码。

jQuery.support.cors = true;
        var para = JSON.stringify({ orgname: var0, ccGuid: var1 , accountTypeGuid: var2 });

$.ajax({
        type: "POST",
        url:http://example:5052/WebServices/CustomerView.asmx/GetCustomerPlans,
        async: true,
        contentType: "application/json; charset=utf-8",
        data: para,
        dataType: "json",           
        xhrFields: {
            'withCredentials': true
       },
        success: UpdateCustomerViews,
        error: fail
    });

1 个答案:

答案 0 :(得分:0)

请尽量不要使用JSON.stringify

试试这个

var para = {"orgname":"abc","ccGuid":"4194716f-a068-e411-80c1-0050568c48b6","accountTypeGuid":"a53cd1ca-716a-e411-80c2-0050568c48b6"}

您的堆栈跟踪显示您的参数格式不正确。