6

My code is:

var test = "it isn't working";
var response = $.ajax({
    type: 'GET',
    url: 'jquerydemo.php', //This is in the same site as the page calling this request, so it's not a same-domain error.
    success: function(){
            test = "it's working";
        },
    error: function(){
            alert("Error detected");
        }
 }).responseText;
alert(test);

I tested the status code and it came out to a 200 and the error function never goes off, but neither does the success function. Like I said in my comment, it isn't a same-origin policy error. It just stays saying "it isn't working". What's going on here?

5 Answers 5

15

Try this:

 error: function(jqXHR, textStatus, errorThrown) {
        console.log(JSON.stringify(jqXHR));
        console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
 }
Sign up to request clarification or add additional context in comments.

Comments

5

Your ajax call is asynchronous. It has not completed yet when your alert at the end runs. Put an actual alert in the success function and you should see your result there.

Remember that making the ajax call just starts the asynchronous ajax call and then the rest of your code continues to run. In the code example you posted, that means your alert(test) call runs right away before the ajax call has completed.

You can ONLY examine the results of the ajax call from within the success handler itself.

var test = "it isn't working";
var response = $.ajax({
    type: 'GET',
    url: 'jquerydemo.php', //This is in the same site as the page calling this request, so it's not a same-domain error.
    success: function(){
            alert("it's working");   // put this here and you will see it 
                                     // if the ajax call is successful
        },
    error: function(){
            alert("Error detected");
        }
 }).responseText;

5 Comments

You also have the option of adding async: true to the settings you pass in your ajax call, but keep in mind that this will halt that entire thread of execution in your script until the request has returned.
@SchiefGelaufen - I assume you meant async: false. Turning the ajax call into a synchronous call is indeed an option, but because it locks up the whole web page for the duration of the ajax call, it is rarely the best design choice. It is more complicated to code with async: true, but gives a better user experience.
Whoops, silly typo on my part. Yes, async: false. It's not necessarily true that making a synchronous ajax request locks up the entire page (at least, there are a number of circumstances under which it doesn't), but it's certainly considered better practice to put any logic relying on the response in your success callback.
@SchiefGelaufen - when would async: false not lock up your page until the call completed?
The particular situation I was thinking of does lock up the page after all. Sheepishly withdrawn.
2

To debug these types of things, I find Firebug an indispensable tool. It will show you exactly the response from the server (500 error, 553 error, what have you). You can put break points in your Javascript code and debug it step by step. Firebug works on Firefox.

For IE, you can use the Developer Tools feature which is similar to Firebug, specially on IE9 which seems more mature than previous versions of the Developer Tools for IE7 or IE8.

1 Comment

You can also just press CTRL-SHIFT-J in Chrome
1

Move that alert(test) from end into the success function of the ajax call. If it alerts it means code is working else it is not. you can only debug ajax call on its success return.

var test = "it isn't working";
var response = $.ajax({
    type: 'GET',
    url: 'jquerydemo.php', 
    success: function(){
            test = "it's working";
            alert(test);   //It will alert when you ajax call returns successfully.
        },
    error: function(){
            alert("Error detected");
        }
 }).responseText;

Hope this helps.

Comments

0

You can make it like

  var response = $.ajax({
    type: 'GET',
    url: 'jquerydemo.php', 
    success: function(){
           alert("it's working");
        },
    error: function(){
            alert("Error detected");
        }
 }).responseText;

This will work....

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.