Sam Grondahl
Sam Grondahl

Reputation: 2467

Can't get response from jQuery AJAX request

When I want clients to stop long-polling, I send back an AJAX response of

"HUPWAIT"

And this is what the response looks like in firebug.

The response in FF.

But when I try to grab the AJAX response in my js, both of the following return false in Firefox and IE9:

$.ajax({
  url: "/wait",
  success: function(data) {
      console.log(data == "\"HUPWAIT\""); //returns false
      console.log(data == "HUPWAIT"); //also returns false
});

Interestingly, the same code returns true in Chrome:

$.ajax({
  url: "/wait",
  success: function(data) {
      console.log(data == "\"HUPWAIT\""); //returns true in Chrome
});

And when I try to debug in Firefox (with firebug) using the following code, I get a weird response:

$.ajax({
  url: "/wait",
  success: function(data) {
      console.log(data); //returns Document
      console.log(''+data); //returns [object XMLDocument]
});

The headers look normal:

HTTP/1.1 200 OK
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Transfer-Encoding: chunked
Via: XXXXXXXXXX
Date: Mon, 10 Sep 2012 23:40:34 GMT
Server: nginx/1.2.3

But there is something weird in the XML tab of firebug:

XML Parsing Error: syntax error Location: moz-nullprincipal:{16915058-4ead-41ef-a63e-1265ee278d74} Line Number 1, Column 1:
"HUPWAIT"
 ^

Any suggestions?

Upvotes: 1

Views: 4324

Answers (3)

jermel
jermel

Reputation: 2336

You could also check the raw responseText:

$.ajax({
    url: "/wait",
    success: function(data, status, jqXHR) {
    console.log(jqXHR.responseText == "\"HUPWAIT\"");
    console.log(jqXHR.responseText == "HUPWAIT");
    console.log(jqXHR.responseText);
});

Upvotes: 0

Ethan Brown
Ethan Brown

Reputation: 27282

Have you tried specifying dataType in the AJAX call?

$.ajax({
    url: "/wait",
    dataType: "text",
    success: function(data) {
        console.log(data == "\"HUPWAIT\""); //returns false
        console.log(data == "HUPWAIT"); //also returns false
});

Upvotes: 1

a.drew.b
a.drew.b

Reputation: 84

Firefox/IE will attempt to parse the AJAX (X=XML) response as an XML document, producing the error you see in the debugger. Adding a "Content-Type" header to the response (text/plain) will force the browser to process the text correctly.

Upvotes: 3

Related Questions