我的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