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, IDictionary2 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;
}
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
jsonvariable in yourJSON.stringify()call defined? It looks like that should bedata, given the code you’ve shown us.