"Message": "Invalid web service call, missing value for parameter: \ u0027haha \ u0027

I am new to jQuery Ajax. I need your help. I want to display one text next to a span element. I already made a link to some topics, but I can’t resolve it.

Here is my error in firebug (new lines and padding added)

{"Message":"Invalid web service call, missing value for parameter: \u0027haha\u0027.", "StackTrace":" at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters) at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters) at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams) at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)", "ExceptionType":"System.InvalidOperationException"} 

In aspx

 <asp:TextBox ID="txtNoiDung" runat="server" TextMode="MultiLine" CssClass="txtNoiDung"></asp:TextBox><span id="vltxtNoiDung"></span> 

In code

  [WebMethod()] public static string test1cai(string haha) { return haha; } 

In javascript

 $(".txtNoiDung").focusout(function () { var dataToSend = { names: $(this).val() }; $.ajax({ type: "POST", url: "QuanLyTin.aspx/test1cai", data: JSON.stringify(dataToSend), contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { $("#vltxtNoiDung").text(msg.d) }, error: function (xhr, reason, ex) { alert(reason); } }); }); 

Thanks in advance!

+4
source share
2 answers

Change data: JSON.stringify(dataToSend), to

 data: JSON.stringify({ haha: $(".txtNoiDung").val() }), 

This assumes that $(".txtNoiDung") is unique on the page; if not, you will need another mechanism to get the value. I'm sure you can get away with $(this).val()) to get the value in this case.

+7
source

The parameter name from the method must always match the parameter passed in the data for the Ajax call. instead of names in using haha ​​data (parameter from C # method).

0
source

Source: https://habr.com/ru/post/1379889/


All Articles