我正在尝试使用带有复杂对象作为输入的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
}
有谁知道为什么会出错?以下是我得到的回复:
{“消息”:“无效的Web服务调用,缺少参数值: \ u0027search \ u0027。“,”StackTrace“:”at System.Web.Script.Services.WebServiceMethodData.CallMethod(对象 target,IDictionary
2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary
2个参数)\ r \ n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext的 context,WebServiceMethodData methodData,IDictionary`2 rawParams)\ r \ n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext的 context,WebServiceMethodData methodData)”, “ExceptionType”: “System.InvalidOperationException”}
答案 0 :(得分: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
?
答案 1 :(得分:0)
事实证明,当您发送GET请求时,不应使用JSON.stringify()
。更改应用程序以发送/接收POST请求解决了问题。
方法签名现在如下所示:
[WebMethod(EnableSession = true)]
//[ScriptMethod(UseHttpGet = true)]
public List<ClientInventory> GetInventory(Search search)
{}
并且js中的request
fn现在将该方法作为“POST”传递。