我的WebMethod看起来是这样的:为什么我无法使用GET将参数发送到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”:“无效的Web服务调用, 缺少参数:\ u0027hello \ u0027的值。”, “堆栈跟踪”:”在 System.Web.Script.Services.WebServiceMethodData.CallMethod(对象 目标,IDictionary的
2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary
2个参数)\ r \ n在 System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext的 上下文中,WebServiceMethodData methodData,IDictionary`2 rawParams个)\ r \ n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的 上下文,WebServiceMethodData methodData)”, “ExceptionType”: “System.InvalidOperationException”} | undefined
我不明白为什么我得到这个错误。
如果我更改jQuery调用使用POST
方法和使UseHttpGet=false
,那么它的工作很好。但我希望它能与GET
一起工作。什么需要解决?
尝试.asmx/HelloWorld?你好= sarfaraz – sri