Asp.net

‘Message’:‘無效的 Web 服務呼叫,參數缺失值:u0027

  • March 19, 2019

當我將 2 個參數從 jQuery 發送到 WebMethod 並使用多個參數時,出現此錯誤。

{"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;
       }

我該如何解決?謝謝你們。

您的服務正在接受名為hahaand的參數tuan,但您的 JavaScript 正在傳入hahasand tuans。從兩者中刪除“s”:

data: JSON.stringify({ haha: $(this).val(),tuan: "hahaha" }),

另外,請記住,這些參數在客戶端和伺服器端之間非常匹配,並且區分大小寫

您的 JavaScript 對象屬性名稱必須與 Web 服務方法上的參數名稱匹配,以便它們可以適當地綁定。您目前擁有:

{ hahas: $(this).val(),tuans: "hahaha" }

這可能應該是:

{ haha: $(this).val(), tuan: "hahaha" }

引用自:https://stackoverflow.com/questions/8032276