0

hi i had this function in my previous php site for posting data and getting response on page, but i can't figure it out in aspx page (not using mvc). here is the code:

$.post('contact.aspx/submitdata', data, function () {
    alert_msg.fadeOut();
    form.find('.msg-thanks').fadeIn(function () {

        textarea.val(textarea.attr('title'));

        window.setTimeout(function () {
            form.find('.msg-thanks').fadeOut();
        }, 5000);

    });
});

previously it was:

$.post('mail/send.php', data, function () {
    alert_msg.fadeOut();
    form.find('.msg-thanks').fadeIn(function () {

        textarea.val(textarea.attr('title'));

        window.setTimeout(function () {
            form.find('.msg-thanks').fadeOut();
        }, 5000);

    });
});

An the worst part is the method invoked doesnt come into debugging calls. here is the method:

[WebMethod]
public void SubmitData(string[] data) {
    //do something;
}
TravisO
  • 9,062
  • 3
  • 33
  • 44
Alok
  • 755
  • 11
  • 35
  • 1
    If this is webforms, you can't just call a method on a page like that. You will have to implement page load to get the formdata you're submitting and handle it. – Uatec Sep 19 '13 at 13:55
  • ok, that much i assumed after two days straight work. But i am new to jquery and their must be some way to do this? – Alok Sep 19 '13 at 14:01

1 Answers1

0

The webmethod should be static. You also need to set content type.

[WebMethod]
public static void SubmitData(string[] data) {
    //do something;
}

$.ajax({
           url: "contact.aspx/submitdata",
           type: "POST",
           contentType: "application/json; charset=utf-8",
           data: data
      })
      .done(function (data) { //do something here on success})
      .fail(function (jqXHR, status) { //error });
Taimur Khan
  • 601
  • 7
  • 20
  • what is the type of data you are passing, xml or json ? – Taimur Khan Sep 19 '13 at 14:10
  • 1
    please read the following post http://stackoverflow.com/questions/7971393/passing-array-of-strings-to-webmethod-with-variable-number-of-arguments-using-jq – Taimur Khan Sep 19 '13 at 14:15
  • thanks for the link i am passing data as array of strings, for sending mail. let me try it as i am new to jquery ajax. i will notify you how it worked for me. – Alok Sep 19 '13 at 16:18
  • alright it worked ,so now how to get the data from textfields and textarea from form? – Alok Sep 19 '13 at 16:29
  • getting data from textfields and textarea is very simple using jquery there are hundreds of tutorial you can find on the internet, just google it. – Taimur Khan Sep 20 '13 at 03:39
  • yeah i got them but now their is another hurdle the webservice is getting hit two times, weird right? – Alok Sep 20 '13 at 14:24