There may be several fixes, first of all you need the document.ready event or another helper function (I switched to the helper function due to the asp.net clientid function), and then you need to stop the submit button of your page.
script type="text/javascript"> var onSubmitClick = function() { var button = this; var obj = {}; obj.name = $.trim($("[id*=txtName]").val()); obj.age = $.trim($("[id*=txtAge]").val()); $.ajax({ type: "POST", url: "CS.aspx/SendParameters", data: JSON.stringify(obj), contentType: "application/json; charset=utf-8", dataType: "json", success: function (r) { alert(rd); } }); return false; }; </script> </head> <body> <form id="form1" runat="server"> <div> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> Name: </td> <td> <asp:TextBox ID="txtName" runat="server" Text = "" /> </td> </tr> <tr> <td> Age: </td> <td> <asp:TextBox ID="txtAge" runat="server" Text = ""/> </td> </tr> <tr> <td> <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClientClick="return onSubmitClick();" UseSubmitBehaviour="false" /> </td> </tr> </table> protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string SendParameters(string name, int age) { return string.Format("Name: {0}{2}Age: {1}", name, age, Environment.NewLine); }
source share