0

I have several requests like this:

   $('#some-button').on('click', function(e) {
      e.preventDefault(); 
      $this = $(this);
      $.ajax({
         url: 'some_request.php/?q='+$some_id,
         dataType: 'json',
         success: function(data) {
            alert('success!')
         }          
      }); 
   });

I.e. lots of AJAX requests that get initiated on button clicks. This request stays 'pending' according to Chrome - no JS alert. No HTTP response code comes back. I think it's just sat there waiting on a response and not getting it.

Question is similar to: jquery $.ajax request remains pending but the answer doesn't help. Nowhere in my PHP or HTML code am I using the session.

Any ideas? Many thanks.

Community
  • 1
  • 1
ale
  • 11,636
  • 27
  • 92
  • 149
  • 1
    try setting `timeout` option in `$.ajax`. I'm guessing is server side code problem, and it;s not returning a 500 status – charlietfl Nov 24 '12 at 16:38
  • Ok, I do get a timeout.. what should I do? Resend the request after waiting some given time? – ale Nov 24 '12 at 16:44
  • 1
    likely a server code problem that needs debugging. Can also add some AJAX error handling as well. See docs. Also inspect request in browser console to make sure `$some_id` is defined and is being sent – charlietfl Nov 24 '12 at 16:46

2 Answers2

2

I think you need a jQuery Ajax Error Handling Function. Here it is:

// jQuery Ajax Error Handling Function
$.ajaxSetup({
    error: function(jqXHR, exception) {
        if (jqXHR.status === 0) {
            alert('Not connect.\n Verify Network.');
        } else if (jqXHR.status == 404) {
            alert('Requested page not found. [404]');
        } else if (jqXHR.status == 500) {
            alert('Internal Server Error [500].');
        } else if (exception === 'parsererror') {
            alert('Requested JSON parse failed.');
        } else if (exception === 'timeout') {
            alert('Time out error.');
        } else if (exception === 'abort') {
            alert('Ajax request aborted.');
        } else {
            alert('Uncaught Error.\n' + jqXHR.responseText);
        }
    }
});

Now call your ajax function:

$.ajax({
    url: 'some_request.php/?q=' + $some_id,
    dataType: 'json',
    success: function(data) {
        alert('success!')
    }
}); 

And check what error you are getting.

0

Are you sure that the Php file you're requesting is on the same domain? Usually this happens when doing cross domain Ajax calls which is not allowed by most web browsers. You'll have to try jsonp instead if that's the case. Here is a nice practical explanation with examples: http://www.jquery4u.com/json/jsonp-examples/

Ayman Farhat
  • 2,010
  • 3
  • 17
  • 16