Web 服务调用无效,参数“filters”缺少值

发布于 2024-12-12 05:50:39 字数 2382 浏览 0 评论 0原文

当我将 ajax 数据发布到我的网络服务时,我不断收到此错误。我已经尝试了很多次并更改了我的代码以返回 json 字符串,但每次都失败。我想做的就是发回我发布的数据,所以我知道它有效。我已经寻找答案,并且尝试了其他替代方法,但我没有得到结果,这里是错误

{"Message":"无效的 Web 服务调用,缺少参数值:\u0027filters\u0027。","StackTrace":" System.Web.Script.Services.WebServiceMethodData.CallMethod(对象目标,IDictionary2 个参数)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象目标,IDictionary2 个参数)\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, 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; 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-12-19 05:50:39

ASP.NET 将自动为您处理 JSON 序列化。更改服务器端方法以匹配从客户端传入的数据类型。

编辑:正如 Jon 指出的那样,您的数据参数的属性键需要与 WebMethod 的输入参数名称匹配(甚至区分大小写)。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<dvals> GetProductsAndFilters(List<dvals> dvals)
{
  // Do what you need to with "filters" here.

  return filters;
} 

另外,您的 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).

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<dvals> GetProductsAndFilters(List<dvals> dvals)
{
  // Do what you need to with "filters" here.

  return filters;
} 

Also, where is the json variable in your JSON.stringify() call defined? It looks like that should be data, given the code you've shown us.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文