【问题标题】:"Message":"Invalid web service call, missing value for parameter: \u0027name\u0027 [duplicate]“消息”:“无效的 Web 服务调用,参数缺失值:\u0027name\u0027 [重复]
【发布时间】: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


【解决方案1】:

我发现了问题。 通过 ajax 使用 GET 函数时,您不需要发送 json,因为 Jquery 会为您完成。 所以你需要删除 contentType 属性并使数据变成这样:

data: { name: "foo", company: "bar", country: "foo", email: "bar", msg: "bla" },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2011-09-22
    相关资源
    最近更新 更多