Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Setting the <code>dataType</code> as "json" means the response from <code>get.php</code> is parsed as JSON. If it's not valid JSON or the response is empty, the request will fail.</p> <p>If the URL is incorrect (can't be found...HTTP 404 error), then the request will fail.</p> <p>The default <code>type</code> of request is "GET", so if <code>get.php</code> doesn't allow "GET" (for some reason), it will return an HTTP error, and the request will fail.</p> <p>If there's an error on the server, it will likely return an HTTP 500 error, and the request will fail.</p> <p>Something to help debug would be to add the <code>error</code> option to the <code>$.ajax</code> call and see if that's called. Instead, I use the <code>.fail()</code> method...it does the same thing.</p> <p>Of course, the more direct way of debugging is opening your browser's console and viewing the AJAX request. It should show multiple details about it, that can help you determine any problems.</p> <p>It might seem as if the AJAX request was never executed/sent, because you don't see the <code>alert</code> in the middle. Well, just because the request wasn't successful, doesn't mean it was skipped. There are plenty of reasons (I named several above) why the request may fail. And the <code>.fail()</code> method will help you determine the cause.</p> <p>Also, the universal convention for handling deferred objects in jQuery is to use the <code>done</code> and <code>fail</code> methods, so that is an option. Of course, <code>$.ajax</code> has specific options you can specify (<code>success</code>, <code>error</code>, and <code>complete</code> - which is for something else), so that is also an option. You can also use special methods (<code>.success()</code>, <code>.error()</code>, <code>.complete()</code>) that are part of the object returned from <code>$.ajax</code>, but those are deprecated as of version 1.8 - take a look at the <code>.ajax</code> docs towards the bottom - <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">http://api.jquery.com/jQuery.ajax/</a> . But here's how I'd set it up, which shouldn't be any different from yours, but does catch errors:</p> <pre><code>$.ajax({ url: 'get.php', dataType: 'json' }).done(data) { console.log("successful response"); }).fail(jqXHR, textStatus, errorThrown) { console.log("error: " + textStatus + ", " + errorThrown); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload