0

I am trying to patch up a bit of AJAX that doesn't work in Chrome. It's not my script and I'd rather not convert the whole thing to jQuery.

xmlhttp.open("GET", query, true);
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4) {
        console.log(xmlhttp.responseText);
    }
}
xmlhttp.send();

This actually outputs something in the log while using Internet Explorer, but when in (Ch)rome I get an empty string, since the JS debugger tells me that GET failed. The target URL is correct since it works in IE, but it is some kind of ASP.NET script which inner workings I'm not very familiar with (I do the HTML, CSS and JavaScript around here).

Why could it be that IE is allowed to retrieve the information from the target, but not Chrome? When using Chrome xmlhttp is a XMLHttpRequest and IE gets some kind of ActiveXObject.

Edit. I found some possible solutions here. Response.Close(), no async or specified/empty data type. It's jQuery though.

Edit #2. What Chrome is saying about my headers.

Community
  • 1
  • 1
Viktor
  • 487
  • 2
  • 8
  • 26
  • If you want to debug, I suggest you look at the http-level, using Wireshark or similar. – troelskn Aug 15 '12 at 10:39
  • What error do you get from the failed GET? Use the status/statusText properties to check this – codebox Aug 15 '12 at 10:41
  • Does the server send the correct header `Content-Type: text/plain`? – Andreas Aug 15 '12 at 10:43
  • @codebox `xmlhttp.status = 0, xmlhttp.statusText = ''` according to the console log. – Viktor Aug 15 '12 at 10:47
  • @Andreas I doubt it. The query is `?id=...`, that is it's the same page that the AJAX executes from, but with appended delimiter and HTML on the bottom, which is split to retrieve the new content that should be made visible by `innerHTML` on a element. I would guess it uses whatever header HTML uses. – Viktor Aug 15 '12 at 10:51
  • @Viktor That was not a question about your opinion ;) Use the developer tools or Fiddler to check the sent header :) – Andreas Aug 15 '12 at 11:01
  • @Andreas I understand that, but I am not a very experienced Chrome developer and can for the life of me not find it. I checked Firefox' page info and it said `text/html`. – Viktor Aug 15 '12 at 13:30
  • Okay, so I've checked now and the headers that are set are: `Page.Response.ContentType = "text/html"; Page.Response.Charset = "utf-8";` Changing to `text/plain` doesn't help, Chrome still fails with the same error. – Viktor Aug 20 '12 at 08:14

2 Answers2

3

Can you view the url by typing it into the address bar in Chrome? There could just be something about your browser config that prevents Chrome from seeing the server.

Another possibility is that you are being scuppered by the Same Origin Policy - is the url you are accessing on the same domain as the page?

codebox
  • 19,927
  • 9
  • 63
  • 81
  • Yes, I can. It's a blank page, but when viewing the source all the HTML is there. Like so: `AJAXDELIMITER`. Firefox opens it with `view-source`, but says the content type is `text/html`. – Viktor Aug 15 '12 at 10:56
  • What is the Content-Type header that gets sent? (you can check this in Chrome Developer Tools) – codebox Aug 15 '12 at 10:58
  • Again, how do I do that? I do not have access to the project here, but I should be able to see what header stackoverflow.com sends, eh? Regarding same origin policy: yes, it is. It's the same file as the JS is included in actually. The `query` variable in my code holds a string in the form of `?id=42&ajaxcall=1`. Accessing this in the browser gives me the full HTML code for the entire page followed by the HTML that the AJAX script should output. Very poor design, as the AJAX request is heavier than the whole page. – Viktor Aug 15 '12 at 13:37
  • Open Dev Tools in Chrome (Tools > Developer Tools) click on the Network tab, send the AJAX request (you should see a new row appear in the Network tab) and check the value in the Type column – codebox Aug 15 '12 at 13:53
  • Aha, there. I checked our demo (which should have the same AJAX implementation) and yes, it is `text/html`. [Screenshot.](http://db.tt/RRdHKIM2) – Viktor Aug 15 '12 at 14:58
  • The headers that are set are: `Page.Response.ContentType = "text/html"; Page.Response.Charset = "utf-8";` No change when using `text/plain`. – Viktor Aug 20 '12 at 08:15
  • I've updated my original post with a screenshot of the headers sent by the page. – Viktor Aug 20 '12 at 09:19
0

Okay, so I've solved it. Chrome apparently dislikes having Response.Close() at the end of the requested page. Changing to Response.End() fixed it. Thank you for your help!

I also replaced that JavaScript with one line of jQuery.

Viktor
  • 487
  • 2
  • 8
  • 26