0

我正在尝试使用带有复杂对象作为输入的 jQuery 调用 ASP Web 服务。

这是我的 jQuery fn:

    request: function (url, method, data) {
        var json = JSON.stringify(data);
        return $.ajax({
            url: url,
            type: method,
            data: json,
            error: ErrorHelpers.printErrorToConsole,
            dataType: 'json',
            contentType: 'application/json',
            processData: false
        });
    }

传入的 json 如下所示:

{
    "search": {
        "WarehouseId": "",
        "AuctionId": "",
        "Barcode": "",
        "Name": "",
        "CategoryId": "",
        "Description": "",
        "ManufacturerId": "",
        "StatusId": "",
        "StatusOperator": "",
        "HasPhoto": "",
        "DateReceived": "",
        "SellerAdministrativeArea": "",
        "SellerId": "",
        "IsApproved": "",
        "Keyword": "",
        "SortBy": "",
        "RowStart": "",
        "RowLimit": "10"
    }
}

我的网络方法定义如下:

    [WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = true)]
    public List<ClientInventory> GetInventory(string search)
    { //code
    }

有谁知道为什么会出错?这是我得到的回复:

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

4

2 回答 2

0

事实证明,JSON.stringify()您在发送 GET 请求时不应该使用。更改应用程序以发送/接收 POST 请求解决了该问题。

方法签名现在如下所示:

    [WebMethod(EnableSession = true)]
    //[ScriptMethod(UseHttpGet = true)]
    public List<ClientInventory> GetInventory(Search search)
    {}

并且requestjs 中的 fn 现在将方法作为“POST”传递。

于 2013-10-01T18:39:14.757 回答
0

你应该创建一个类...

public class Search
{
    public string WarehouseId { get; set; }
    public string AuctionId { get; set; }
    public string Barcode { get; set; }
    public string Name { get; set; }
    public string CategoryId { get; set; }
    public string Description { get; set; }
    public string ManufacturerId { get; set; }
    public string StatusId { get; set; }
    public string StatusOperator { get; set; }
    public string HasPhoto { get; set; }
    public string DateReceived { get; set; }
    public string SellerAdministrativeArea { get; set; }
    public string SellerId { get; set; }
    public string IsApproved { get; set; }
    public string Keyword { get; set; }
    public string SortBy { get; set; }
    public string RowStart { get; set; }
    public string RowLimit { get; set; }
}

并像这样修改您的方法...

[WebMethod(EnableSession = true)]
    [ScriptMethod(UseHttpGet = true)]
    public List<ClientInventory> GetInventory(Search search)
    { //code
    }

另外,请检查您在此处使用的方法...

type: method,

它在错误中表明您正在使用Post?

于 2013-10-01T17:05:09.100 回答