使用jQuery调用Web服务方法时,获取“无效的Web服务调用,缺少参数值”错误

时间:2010-09-20 20:27:57

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

这是我的aspx文件代码:


var DTO = { "'productCategoryId'": "'10'" };
$.ajax({
 type: "GET",
 url: "/WebService/DsmWebServices.asmx/GetProductSubCategory",
 data: DTO,
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 success: function (msg) {
  window.alert("success");
 },
 error: function (msg) {
 }
});

这是我的网络服务方法:


[WebMethod,ScriptMethod(UseHttpGet=true)]
public bool GetProductSubCategory(int productCategoryId)
{
 //do some stuff
 return true;
}

问题是jQuery函数失败并出现以下错误:


{"Message":"Invalid web service call, missing value for parameter:
\u0027productCategoryId\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"}

通过使用Firebug,请求的URL jQuery是


http://192.168.1.100:8888/WebService/DsmWebServices.asmx/GetProductSubCategory?%27productCategoryId%27=%2710%27

2 个答案:

答案 0 :(得分:1)

你必须在你的ajax调用中使用“post”作为类型。此外,传递给Web服务的数据需要是json字符串。不是json对象。看一下json2库的stringify方法:

http://www.json.org/js.html

答案 1 :(得分:1)

我收到的其他错误消息也一样。由于我最终使用帖子,我想向读者指出我的另一个答案:Calling Webservice with Jquery causing woe