“消息”:“无效的Web服务调用,缺少参数值:\ u0027
当我从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"}
在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); } }); });
在代码背后
[WebMethod()] public static string test1cai(string haha, string tuan) { return "Hi, "+haha + tuan; }
我该如何解决? 谢谢你们。
您的服务正在接受名为haha
和tuan
参数,但您的JavaScript正在传递tuans
和tuans
。 从两者中删除“s”:
data: JSON.stringify({ haha: $(this).val(),tuan: "hahaha" }),
另外,请记住,这些参数在客户端和服务器端之间非常匹配,区分大小写 。
您的JavaScript对象属性名称必须与Web服务方法上的参数名称匹配,以便可以适当地绑定它们。 你目前有:
{ hahas: $(this).val(),tuans: "hahaha" }
应该是:
{ haha: $(this).val(), tuan: "hahaha" }
您应该在Ajax调用中从代码behinedto中的函数传递相同的方法参数
数据:“{‘哈哈’:’”+“您的数据”+’tuan’:’“+”您的数据“+”’}“
- 不应该有像’tuan’这样的空格