1

I'm trying to make an AJAX request with dataType: "json" and I'm Firebug keeps showing the request as "aborted."

POST http://mydomain.com/path/to/page Aborted

This is the snippet:

var postData = "someVar=someValue&otherVar=otherValue", msg;
$.ajax({
    type: "POST",
    url: "/path/to/page",
    data: postData,
    dataType: "json",
    cache: false,
    success: function(response) {
        if (typeof response.row != undefined) {
            $('#my-select')
                .append($('<option></option>')
                    .attr("value", response.row.id)
                    .text(response.row.name)
                );
            msg = response.msg;
        } else {
            msg = 'failed';
        }
        alert(msg);
    },
    error: function(xhr, status, thrown) {
        // EDIT 1
        alert(status);  // <-- It's alerting "timeout"
    }
});

Incidentally, I read a bunch of questions on this site of people with similar issues, but may of them turned out to be very specific. One was because the request was too large, another was because they were requesting to another domain, etc etc.

I get no response headers and no response body.

Here are my request headers (simplified):

Host                mydomain.com
User-Agent          Firefox/8.0.1
Accept              application/json, text/javascript, */*; q=0.01
Accept-Language     en-us,en;q=0.5
Accept-Encoding     gzip, deflate
Accept-Charset      ISO-8859-1,utf-8;q=0.7,*;q=0.7
DNT 1
Connection          keep-alive
Content-Type        application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With    XMLHttpRequest
Referer             http://mydomain.com/path/to/page
Content-Length      125
Cookie              PHPSESSID=somerandomstring

Nothing shows up in the Apache error_log. I navigated manually to that page in the browser and I didn't get any errors (just a white screen and some text).

NOTE: This works on my localhost but not on the testing server.

Edit: Added error() callback and it's alerting "timeout."

Yes Barry
  • 9,514
  • 5
  • 50
  • 69
  • I had this recently.. wracking my brain to remember the problem and solution. You aren't hitting an IIS box, are you? – Matt H Dec 16 '11 at 20:10
  • @MattH No, it's on a Linux Apache server. – Yes Barry Dec 16 '11 at 20:12
  • Might also have been an SSL cert problem. I finally tracked it down by using Tamper Data rather than Firebug, though.... at least that's what I used for the last time I had ajax issues. – Matt H Dec 16 '11 at 20:15
  • Ok for the time being I'll install that, thanks. Any other ideas? Before today it worked, and I _wasn't_ setting `dataType: "json"` before (e.g. plain text/html response before). Is there a problem with _that_? – Yes Barry Dec 16 '11 at 20:21
  • Hmmm... I don't use `dataType: "json"` with `$.ajax`, rather I use `$.getJSON`. I would only suspect a problem with that if you were trying to get `ajax` from a different domain. – Matt H Dec 16 '11 at 20:30
  • Does the Apache access log show that you are even hitting that page? – Cthos Dec 16 '11 at 22:17
  • @Cthos Yes, it shows in the access_log as 200.. – Yes Barry Dec 16 '11 at 22:25
  • Is it actually timing out? How long does the request take in firebug? Have you tried setting `timeout: 3000` or something? – Cthos Dec 16 '11 at 22:38
  • @Cthos Oh man.. Yeah that worked u.u lol. Why would it timeout _so fast_ though? It doesn't timeout on my localhost and it's not a heavy request. It _should_ run super fast... Please transfer your comment to an answer. – Yes Barry Dec 16 '11 at 22:48
  • @mmmshuddup - Great question, firebug should tell you how long the request actually takes, but there's no real telling what that default value is. – Cthos Dec 16 '11 at 22:56
  • @Cthos Gotcha yeah.. Well thanks again for helping me! (Something I _should_ have tried on my own lol) – Yes Barry Dec 17 '11 at 02:42

1 Answers1

2

The request could acutally be timing out.

First thing to try would be to increase the default timeout value to something a little higher, like so:

$.ajax({
    type: "POST",
    url: "/path/to/page",
    data: postData,
    timeout: 3000,
    dataType: "json",
    [...]

The default timeout value is dependent on the browser, apparently, so it could be lower than your script execution time, or set by something else, etc.

JQuery ajax call default timeout value

Community
  • 1
  • 1
Cthos
  • 985
  • 6
  • 10