【问题标题】:Why am I unable use GET to send parameters to WebMethod?为什么我无法使用 GET 向 WebMethod 发送参数?
【发布时间】:2012-09-21 02:31:43
【问题描述】:

我的 WebMethod 如下所示:

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json, UseHttpGet=true)]
public List<Person> HelloWorld(string hello)
{
   List<Person> persons = new List<Person> 
   {
      new Person("Sarfaraz", DateTime.Now),
      new Person("Nawaz", DateTime.Now),
      new Person("Manas", DateTime.Now)
   };
   return persons;
}

我正在尝试使用 jQuery 调用此方法:

var params={hello:"sarfaraz"}; //params to be passed to the WebMethod
$.ajax
({
    type: "GET",   //have to use GET method
    cache: false,
    data: JSON.stringify(params),
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    url: "http://localhost:51519/CommentProviderService.asmx/HelloWorld",
    processData: true,
    success: onSuccess,
    error: onError      //it gets called!
});

但它不起作用。它没有调用onSuccess 回调,而是调用onError,其中我使用alert 作为:

alert(response.status + " | " + response.statusText + " | " + response.responseText + " | " + response.responseXML );

打印这个:

500 |内部服务器错误 | {"Message":"无效的网络服务调用, 参数缺失值:\u0027hello\u0027。","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(对象 目标,IDictionary2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 个参数)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext 上下文、WebServiceMethodData 方法数据、IDictionary`2 rawParams)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文,WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} | 未定义

我不明白为什么会出现此错误。

如果我将 jQuery 调用更改为使用 POST 方法并设置为 UseHttpGet=false ,那么效果很好。但我希望它与GET 一起工作。需要解决什么问题?

【问题讨论】:

  • 试试 .asmx/HelloWorld?hello=sarfaraz

标签: jquery asp.net ajax web-services getmethod


【解决方案1】:

HTTP GET 要求参数全部编码在 URL 中。

问题是您在有效载荷上执行JSON.stringifyjQuery.ajax 真的只是在寻找可以变成一系列查询字符串参数的简单字典。

所以如果你有这样的对象:

{ name: "value" }

jQuery 会将它附加到 URL 的末尾,如下所示:

?name=value

使用 Firebug、Chrome 开发者工具或 IE 开发者工具来检查传出的 URL,我怀疑你会看到它是 ASP.Net 无法翻译的格式。

【讨论】:

  • 现在,我尝试使用 { hello : "sarfaraz" },但出现此错误:500 | Internal Server Error | {"Message":"Invalid JSON primitive: sarfaraz.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()....
  • @Nawaz - 尝试将您的数据参数更改为如下所示:data: {"hello":"sarfaraz"}
  • 好吧,自从我用 ASP.Net 做这件事以来已经有一段时间了,但我隐约记得必须将我的数据完全括在引号中。因此,您可以尝试两件事之一。 1.) data: "{'hello':'sarfaraz'}" 2.) 将 URL 中的数据作为查询字符串参数粘贴,并改用 jQuery.get()
【解决方案2】:

要在 GET 中发出 $.ajax 请求,您必须将数据设置为 params="hello=sarfaraz"

所以完整的代码sn-p可以简单地是

var params="hello=sarfaraz"; //params to be passed to the WebMethod
$.ajax
({
    type: "GET",   //have to use GET method
    cache: false,
    data: params,
    dataType: 'json',
    url: "http://localhost:51519/CommentProviderService.asmx/HelloWorld",
    success: onSuccess,
    error: onError    //it gets called!
});

希望有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-06
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多