2015-12-02 102 views
0

我試過這麼多的例子,但仍然卡住。使用jQuery jsonp跨域調用ASP.NET web服務

我想打電話給使用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"} 

顯示在提琴手的網址:

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;   
    } 

錯誤消息的提琴手顯示:

System.InvalidOperationException: Missing parameter: orgname. at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at 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 
    }); 

回答

0

,請儘量不要使用JSON.stringify

試試這個

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

你的堆棧跟蹤顯示,你的參數的格式不正確。

+0

感謝您的快速回復,但建議的解決方案沒有爲我工作。 – Shubh

+0

除去創建JSON.stringify參數串之後: [鏈接](HTTP://示例:5052/Web服務/ CustomerView.asmx/GetCustomerPlans回調= jQuery110203820646793817735_1448975800168&ORGNAME = ABC&ccGuid = 4194716f-A068-e411-80c1-0050568c48b6&accountTypeGuid = 8d89ffdf- 91d7-e411-80d0-0050568c48b6&_ = 1448975800169),現在顯示授權錯誤。 – Shubh