2

I try to make a jQuery.ajax call:

jQuery("#search_form").live("submit", function() {
    search_nr = jQuery("#search_input").val();
    jQuery.ajax({
        url: '/modules/mod_findarticle/process.php',
        data: "search_nr="+search_nr,
        async: true,
        'success':  function(data) {
                        alert(data);
                    },
        'error': function(jqXHR, textStatus, errorThrown) {
                  alert(jqXHR.status+",  " + jqXHR.statusText+",  "+textStatus+",  "+errorThrown);
        }
    });
});

In return i always get an alert with "0, error, error," message. Everything's fine with async=false. I know that with asynchronous call the script finish work before any actual data is recieved but what can be done to avoid this?

9
  • 2
    If you inspect the response in Firebug, what is the error code returned? Commented Oct 30, 2012 at 9:07
  • Why are you even setting it? It is true by default. Commented Oct 30, 2012 at 9:11
  • Not related to the error, but .live is deprecated. You should use .on. Commented Oct 30, 2012 at 9:16
  • What kind of data is the PHP returning? Is it setting the Content-type correctly? Commented Oct 30, 2012 at 9:18
  • Thank you for the replies. So: 1. the error messages are displayed by alert(jqXHR.status+", " + jqXHR.statusText+", "+textStatus+", "+errorThrown); which shows 0, error, error, 2. Indeed, async is enanled by default. If I remove async=true at all, nothing changes. 3. thanks for the comment, but i doubt that the problem is related to .live() 4. I tried to add the dataType parameter to the .ajax call, setting it to "html" or "json" with no effect. Commented Oct 30, 2012 at 9:40

1 Answer 1

3

Problem solved thanks to user mccannf : The data was submitted twice by triggering the 'submit' event on the form, so the script always stopped processing data before the second call. Replacing function() in .live(...) with function(e) {e.preventDefault; ... } disables the default submit action and hence does the trick!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.