我使用Ajax调用服务器端方法,但我一直收到此错误
{"消息":"无效的Web服务调用,缺少参数值:\ u0027name \ 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参数)\ r \ n在System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context,WebServiceMethodData methodData,IDictionary`) 2 rawRarams)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,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)
{
}
答案 0 :(得分:0)
我发现了问题。 当通过ajax使用GET函数时,你不需要发送json,因为Jquery会为你做。 所以你需要删除contentType属性并使数据如下:
data: { name: "foo", company: "bar", country: "foo", email: "bar", msg: "bla" },