Ajax无法仅在IE 10中将参数传递给WebMethod - 缺少参数值:\ u0027recordID \ u0027

时间:2013-09-20 02:32:11

标签: asp.net ajax json internet-explorer-10 webmethod

我一次又一次地搜索但没有希望。我开始认为这是IE 10的错误。因为下面的代码适用于Chrome,Firefox,IE 7 8 9等等。

我收到了这个错误

    GetDataFromWebMethod(): {"Message":"Invalid web service call, missing value for parameter: \u0027recordID\u0027.","StackTrace":" 
    at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n 
    at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams
    (Object target, IDictionary`2 parameters)\r\n 
at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n 
        at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

当我尝试使用如下参数调用ASP.NET WebMethod时:

var dataToSend = '{"recordID":"' + id + '"}';
var URLDataForm = 'FundsMain.aspx/Get';
oriEntity = GetDataFromWebMethod(URLDataForm, dataToSend);

使用这样的函数

function GetDataFromWebMethod(webMethodUrl, dataToSend) {
    if (webMethodUrl == '' || webMethodUrl === undefined) return null;

    var data;
    $.ajax({
        type: 'POST',
        data: dataToSend,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: webMethodUrl,
        async: false,
        cache: false,
        success: function (responseTxt) {
            data = $.parseJSON(responseTxt.d.ResponseData);
        },
        error: function (err) {
            showMessageBox("error", "Error", "GetDataFromWebMethod(): " + err.responseText);
        }
    });
    return data;
}

的WebMethod:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static AjaxResponse Get(int recordID)
{
      // remove for brevity
}

以下调试数据:

 - in Firefox: dataToSend "{"recordID":"1625"}" 
 - in IE <10:  dataToSend "{\"recordID\":\"1625\"}" String
 - in Chrome:  dataToSend:"{"recordID":"1625"}"
 - in IE 10:   dataToSend "{\"recordID\":\"1625\"}" String   **(same as IE <10 !)**

有人对此有任何想法吗?或者我想我错过了什么? 谢谢你的时间!

3 个答案:

答案 0 :(得分:1)

由于网络方法参数为int,请尝试使用以下

var dataToSend = '{"recordID":' + id + '}';

而不是

var dataToSend = '{"recordID":"' + id + '"}';

答案 1 :(得分:0)

此问题似乎是一个IE 10错误。感谢@Damith的帮助:)

答案 2 :(得分:0)

将元标记添加为

<meta http-equiv="X-UA-Compatible" content="IE=9"/>

然后使用

var dataToSend = '{"recordID":' + id + '}';

而不是

var dataToSend = '{"recordID":"' + id + '"}';

希望这在IE 10上运行良好