【发布时间】:2017-10-24 11:26:01
【问题描述】:
我正在使用 Ajax 调用服务器端方法,但我不断收到此错误
{"Message":"无效的 Web 服务调用,缺少参数值:\u0027name\u0027。","StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary@987654322 @2 参数)\r\n 在 System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}
这是客户端
$(document).ready(function () {
$.ajax({
url: "Server.aspx/sendEmail",
contentType: "application/json; charset=UTF-8",
data: JSON.stringify({ name: "foo", company: "bar", country: "foo", email: "bar", msg: "bla" }),
dataType: "json",
success: function (data) {
var a = 3;
},
error: function (a, b) {
var a = 43;
}
});
});
这是服务器端
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string sendEmail(string name, string company, string country, string email, string msg)
{
}
【问题讨论】:
-
第一个没有帮助,第二个没有帮助。您可以看到我在 javascrip 和 webmethod 中具有相同的名称
-
你看到我贴在这里的链接了吗
-
代替这个数据:JSON.stringify({ name: "foo", company: "bar", country: "foo", email: "bar", msg: "bla" }) 使用数据:JSON.stringify({ "name": "foo", "company": "bar", "country": "foo", "email": "bar", "msg": "bla" })跨度>
-
并添加 type :"GET/POST" 属性
标签: javascript c# jquery html ajax