jQuery "Message":"无效的 Web 服务调用,缺少参数值:\u0027
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8032276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Message":"Invalid web service call, missing value for parameter: \u0027
提问by tuan.it89
I got this error when i send 2 parameter from jQuery to WebMethod and using multiple params.
当我从 jQuery 向 WebMethod 发送 2 个参数并使用多个参数时出现此错误。
{"Message":"Invalid web service call, missing value for parameter: \u0027haha\u0027.","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"}
In jQuery:
在 jQuery 中:
$(".txtNoiDung").focusout(function () {
$.ajax({
type: "POST",
url: "QuanLyTin.aspx/test1cai",
data: JSON.stringify({ hahas: $(this).val(),tuans: "hahaha" }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#vltxtNoiDung").text(msg.d)
},
error: function (xhr, reason, ex) {
alert(reason);
}
});
});
In code behind
在后面的代码中
[WebMethod()]
public static string test1cai(string haha, string tuan)
{
return "Hi, "+haha + tuan;
}
How can i resolve it? Thanks you guys.
我该如何解决?谢谢你们。
回答by Dave Ward
Your service is accepting parameters named haha
and tuan
, but your JavaScript is passing in hahas
and tuans
. Remove the "s" from both:
您的服务正在接受名为haha
and 的参数tuan
,但您的 JavaScript 正在传入hahas
and tuans
。从两者中删除“s”:
data: JSON.stringify({ haha: $(this).val(),tuan: "hahaha" }),
Also, keep in mind that these parameters much match between client- and server-side with case-sensitivity.
另外,请记住,这些参数在客户端和服务器端之间非常匹配,并且区分大小写。
回答by C???
Your JavaScript object property names must match the names of the parameters on the web service method so they can be bound appropriately. You currently have:
您的 JavaScript 对象属性名称必须与 Web 服务方法上的参数名称匹配,以便可以适当地绑定它们。您目前拥有:
{ hahas: $(this).val(),tuans: "hahaha" }
which should probably be:
这应该是:
{ haha: $(this).val(), tuan: "hahaha" }
回答by ashveli
You should be passing the same method parameter from the function in code behinedto, in your Ajax call
您应该在 Ajax 调用中从代码 behinedto 中的函数传递相同的方法参数
data: "{ 'haha': '" + "your data" + 'tuan': '" + "your data" + "' }"
数据:“{'哈哈':'”+“你的数据”+'tuan':'”+“你的数据”+“'}”
- There shouldn't be any spaces like ' tuan '
- 不应该有像‘tuan’这样的空格
回答by Gaurav Joshi
Basically you have to use curly brackets while JSON.stringify({}) You have to use like this :
基本上你必须使用大括号而 JSON.stringify({}) 你必须像这样使用:
$.ajax({
type: "POST",
url: url,
data: JSON.stringify({ PrefrencesDetailEntity: PrefrencesDetailEntity
contentType: "application/json;", //charset=utf-8",
dataType: "json",
success: function (data, jqXHR) {
if (data !== null && data.Status === 201) {
toastr.success("Your changes have been saved successfully");
}
}
});
and C# method would be like this :
$.ajax({ type: "POST", url: url, data: JSON.stringify({ PrefrencesDetailEntity: PrefrencesDetailEntity
contentType: "application/json;", //charset=utf-8", dataType: "json", success : function (data, jqXHR) { if (data !== null && data.Status === 201) { toastr.success("您的更改已成功保存"); } } }); 和 C# 方法会像这个 :
[WebMethod] public static NotificationValues PostNotificationData(PrefrencesDetailEntity PrefrencesDetailEntity) { //your C# code logic }
[WebMethod] public static NotificationValues PostNotificationData(PrefencesDetailEntity PrefrencesDetailEntity) { //你的C#代码逻辑}