[英]Error calling ASP.net webservice with jQuery
I'm attempting to call an ASP webservice using jQuery with a complex object as an input. 我正在尝试使用带有复杂对象作为输入的jQuery调用ASP Web服务。
Here is my jQuery fn: 这是我的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
});
}
the json being passed in looks like this: 传入的json看起来像这样:
{
"search": {
"WarehouseId": "",
"AuctionId": "",
"Barcode": "",
"Name": "",
"CategoryId": "",
"Description": "",
"ManufacturerId": "",
"StatusId": "",
"StatusOperator": "",
"HasPhoto": "",
"DateReceived": "",
"SellerAdministrativeArea": "",
"SellerId": "",
"IsApproved": "",
"Keyword": "",
"SortBy": "",
"RowStart": "",
"RowLimit": "10"
}
}
and my web method definition is as follows: 和我的网络方法定义如下:
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public List<ClientInventory> GetInventory(string search)
{ //code
}
Does anyone know why it's erroring out? 有谁知道为什么会出错? Here is the response I get: 这是我得到的答复:
{"Message":"Invalid web service call, missing value for parameter: \'search\'.","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary
2 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"} {“ Message”:“无效的Web服务调用,缺少参数值:\\ u0027search \\ u0027。”,“ StackTrace”:“位于System.Web.Script.Services.WebServiceMethodData.CallMethod(对象目标,IDictionary2 parameters)\\r\\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, 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上下文,WebServiceMethodData methodData)“,” ExceptionType“:” System.InvalidOperationException“}
You should create a class... 您应该创建一个类...
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; }
}
and modify your method like this... 并像这样修改您的方法...
[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public List<ClientInventory> GetInventory(Search search)
{ //code
}
Also, check the method you are using here... 另外,在这里检查您使用的方法...
type: method,
It suggests in the error that you're using a Post
? 错误提示您正在使用Post
?
It turns out that you shouldn't use JSON.stringify()
when you're sending GET requests. 事实证明,发送GET请求时不应使用JSON.stringify()
。 Changing the app to send/receive POST requests solved the problem. 更改应用程序以发送/接收POST请求即可解决此问题。
The method signature now looks like this: 现在,方法签名如下所示:
[WebMethod(EnableSession = true)]
//[ScriptMethod(UseHttpGet = true)]
public List<ClientInventory> GetInventory(Search search)
{}
and the request
fn in the js is now passing the method as a "POST". 并且js中的request
fn现在将方法作为“ POST”传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.