M.B M.B - 3 months ago 9
Javascript Question

jquery ajax does not work in asp.net

I'm in the middle of an asp.net webform project. I want to use jquery ajax.
here is my code:

<asp:Button ID="btn_comment" runat="server" CssClass="contact_btn pull-right" Text="send" OnClientClick="callajax(event)" />


ajax function :

<script type="text/javascript">
function callajax(e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "../Music.aspx/sendcomment",
data: '{name: "' + $("#<%=TextBox1.ClientID%>").text() + '" , email:"' + $("#<%=TextBox2.ClientID%>").text() + '" , website: "' + $("#<%=TextBox4.ClientID%>").text() + '" , comment: "' + $("#<%=TextBox3.ClientID%>").text() + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response.d) {
alert('ok');
}

},
failure: function (response) {
alert('eror');
}
});
}
</script>


sendcomment method :

[WebMethod]
public static void sendcomment(string name , string email , string website , string comment)
{
// do something
}
catch (Exception)
{
}
}


I also add jquery file to my project but when I run the project and click on button it does not work and fire the sendcomment method.
I also use updatepanel but it doesn't work.Can anyone help me?

Answer

Use this as url

url: "Music.aspx/sendcomment",