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

问题描述 投票:0回答:1

我已经尝试了很多示例,但仍然失败。

我正在尝试使用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:缺少参数:组织名称。在System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection集合)System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()在System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

请提出我在哪里犯错。

FYI:下面给出了原始的工作代码,它工作正常,但在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
    });
jquery ajax web-services jsonp
1个回答
0
投票

请尽量不要使用JSON.stringify

尝试一下

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

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

© www.soinside.com 2019 - 2024. All rights reserved.