admin管理员组

文章数量:1332352

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!

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!

Share Improve this question edited Nov 6, 2011 at 16:59 tuan.it89 asked Nov 6, 2011 at 16:37 tuan.it89tuan.it89 3131 gold badge3 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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.

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).

本文标签: