tuan.it89
tuan.it89

Reputation: 313

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

I'm a newbie in Jquery Ajax. I need your help. I want to display one text beside span element. I have already done reference some topics but i cant resolve it

Here is my error in firebug (newlines and indentation 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 behind

  [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!

Upvotes: 4

Views: 4904

Answers (2)

ashveli
ashveli

Reputation: 288

The parameter name from the method should always be same with parameter passed in the data for an Ajax call. instead of names in data use haha(parameter from c# method).

Upvotes: 0

arb
arb

Reputation: 7863

Change data: JSON.stringify(dataToSend), to

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

This is assuming that $(".txtNoiDung") is unique in the page, if it's not, you'll need another mechanism for getting the value. I'm pretty sure you can get away with $(this).val()) to get the value in this case.

Upvotes: 7

Related Questions