Web 服务调用无效,参数“filters”缺少值
当我将 ajax 数据发布到我的网络服务时,我不断收到此错误。我已经尝试了很多次并更改了我的代码以返回 json 字符串,但每次都失败。我想做的就是发回我发布的数据,所以我知道它有效。我已经寻找答案,并且尝试了其他替代方法,但我没有得到结果,这里是错误
{"Message":"无效的 Web 服务调用,缺少参数值:\u0027filters\u0027。","StackTrace":" System.Web.Script.Services.WebServiceMethodData.CallMethod(对象目标,IDictionary2 个参数)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象目标,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 context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
jquery:
data = [{'Name': 'Acer', 'Count': 1 }, {'Name': 'HP', 'Count': 2 }]
function getProducts(json, pageIndex, pageSize) {
$.ajax({
type: 'POST',
url: '/website2/WebServices/GetProducts.asmx/GetProductsAndFilters',
data: "{'data' : {'filters':" + JSON.stringify(json) + ", 'PageIndex' : " + pageIndex + ", 'PageSize' : " + pageSize + "}}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (responseText) {
//alert(responseText.d);
$('.console').html(responseText.d);
},
error: function (xhr, status, error) {
//var msg = JSON.parse(xhr.responseText);
//alert(msg.Message);
$('.console').html(xhr.responseText)
}
});
}
getProducts(data, "0", "2")
C#:
public class dvals
{
public string Name { get; set; }
public string Count { get; set; }
}
public class data
{
public List<dvals> Filters {get;set;}
public string PageIndex { get; set; }
public string PageSize { get; set; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public data GetProductsAndFilters(data filters)
{
JavaScriptSerializer js = new JavaScriptSerializer();
//List<dvals> json = js.Deserialize<List<dvals>>(filters);
//data json = js.Deserialize<data>(filters);
var fname = "";
/*
foreach (var filter in filters)
{
if (filter.Name == "Acer")
{
fname = filter.Name;
break;
}
}*/
return filters;
}
When I post my ajax data to my webservice I keep getting this error. I have tried many times and changed my code to return a json string back but it fails every time.All im trying to do is send back the data I have posted so I know it works. I have looked for answers and I have tried other alternatives but im not getting the results here is the error
{"Message":"Invalid web service call, missing value for parameter: \u0027filters\u0027.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary2 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"}
jquery:
data = [{'Name': 'Acer', 'Count': 1 }, {'Name': 'HP', 'Count': 2 }]
function getProducts(json, pageIndex, pageSize) {
$.ajax({
type: 'POST',
url: '/website2/WebServices/GetProducts.asmx/GetProductsAndFilters',
data: "{'data' : {'filters':" + JSON.stringify(json) + ", 'PageIndex' : " + pageIndex + ", 'PageSize' : " + pageSize + "}}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (responseText) {
//alert(responseText.d);
$('.console').html(responseText.d);
},
error: function (xhr, status, error) {
//var msg = JSON.parse(xhr.responseText);
//alert(msg.Message);
$('.console').html(xhr.responseText)
}
});
}
getProducts(data, "0", "2")
C#:
public class dvals
{
public string Name { get; set; }
public string Count { get; set; }
}
public class data
{
public List<dvals> Filters {get;set;}
public string PageIndex { get; set; }
public string PageSize { get; set; }
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public data GetProductsAndFilters(data filters)
{
JavaScriptSerializer js = new JavaScriptSerializer();
//List<dvals> json = js.Deserialize<List<dvals>>(filters);
//data json = js.Deserialize<data>(filters);
var fname = "";
/*
foreach (var filter in filters)
{
if (filter.Name == "Acer")
{
fname = filter.Name;
break;
}
}*/
return filters;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.NET 将自动为您处理 JSON 序列化。更改服务器端方法以匹配从客户端传入的数据类型。
编辑:正如 Jon 指出的那样,您的数据参数的属性键需要与 WebMethod 的输入参数名称匹配(甚至区分大小写)。
另外,您的
JSON.stringify()
调用中的json
变量在哪里定义?根据您向我们展示的代码,看起来应该是data
。ASP.NET will handle the JSON [de]serialization for you automatically. Change your server-side method to match the type of data you're passing in from the client-side.
edit: And as Jon pointed out, your data parameter's property key needs to match the WebMethod's input parameter name (this is case-sensitive even).
Also, where is the
json
variable in yourJSON.stringify()
call defined? It looks like that should bedata
, given the code you've shown us.