-1

I have JS code which works on IE browser. But it doesn't work in Chrome and Mozilla. What should be changed to make it work on these browsers?

<html>
<head>
<script type="text/javascript">
function readFile(url) {
    pageRequest = new XMLHttpRequest()
    pageRequest.open("GET", url, false);
    pageRequest.send(null);

    return pageRequest.responseText;    
}
</script>
</head>

<body>
<script type="text/javascript">
    var txt = readFile("?GameID=5&from=0&num=50000");
    document.write(txt);
</script>
</body>

</html>






function readFile(url) {
    var request = new XMLHttpRequest();
    request.open("GET", url, false);
    request.send();

    return request;
}

var request = readFile("http://? GameID=5&from=0&num=50000");

request.onload = function() {
    document.write(request.responseText);
};

this code doesnt work neither in IE nor in Chrome)) any other ideas?

  • 8
    Wlecome to [so]. It appears that you've fallen into the trap of the classic async problem -- see [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) for more information on this issue. – Qantas 94 Heavy Dec 03 '13 at 13:56
  • probably need a semi-colon....`pageRequest = new XMLHttpRequest();` – MonkeyZeus Dec 03 '13 at 13:57
  • 1
    @MonkeyZeus: it has nothing to do with that, though I personally use semicolons where required (without ASI). – Qantas 94 Heavy Dec 03 '13 at 13:57
  • 2
    @Qantas94Heavy I don't trust missing semi-colons, ever. – MonkeyZeus Dec 03 '13 at 13:58
  • 1
    If your IE is older than 7, you'll have to use ActiveX for your request. – sheng Dec 03 '13 at 13:58
  • So how is it possible this code works on IE??? – A. Wolff Dec 03 '13 at 14:02
  • @A.Wolff man! My bad. I misread the question, thinking that it *didn't* work in IE, but it worked in Chrome and Firefox, as is usually the case. – sheng Dec 03 '13 at 14:06
  • @ShengSlogar My previous comment was not for you but for OP which said that his code works on IE which surprises me. And if it's true, i'd like to know why/how is it possible – A. Wolff Dec 03 '13 at 14:07
  • @A.Wolff well you still corrected my error, even if you didn't mean to. That explains the lack of the at sign. :) So are you saying his code shouldn't be working at all because of async problems? If so, it might be because IE is *so* slow that it somehow works, or maybe IE runs everything on a single thread. :) – sheng Dec 03 '13 at 14:13
  • "So are you saying his code shouldn't be working at all because of async problems?" Ya, that was what i was talking about :) – A. Wolff Dec 03 '13 at 14:17
  • @A.Wolff great. Just use the at sign (@shengslogar). I wouldn't have seen your comment except I came to this page by accident thinking it was another question. :) – sheng Dec 03 '13 at 14:23
  • @shengslogar oh ya, forget to use it, my bad :( – A. Wolff Dec 03 '13 at 14:27
  • I've answered an extremely similar question here: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call/16825593#16825593 Let me know if that helps you – Benjamin Gruenbaum Dec 03 '13 at 15:17

2 Answers2

1

This occurs because of Cross-Origin Resource Sharing, click here for more details. You need to set the Access-Control-Allow-Origin header on server and define the domains that have permissions to access the resource. I tested your code in a server with Internet Explorer and got the same error.

Humberto Corrêa
  • 789
  • 1
  • 6
  • 17
0

You're having async problems. If the HTTP request didn't finish yet, you won't be able to access request.responseText. This might work in some browsers because of pure chance. So we have to attach an onload event listener.

Try this code:

function readFile(url) {
    var request = new XMLHttpRequest();
    request.open("GET", url, false);
    request.send();

    return request;
}

var request = readFile("http://example.com/displayScore.php?GameID=5&from=0&num=50000");

request.onload = function() {
    document.write(request.responseText);
};

You could also pass readFile a second parameter that is a function like this instead of returning the whole object:

function readFile(url, onload) {
    //load code goes here...

    if (typeof onload == "function") {
        request.onload = function(){
            onload(request),
        };
    }
}

readFile("url", function(e){
    document.write(e.responseText);
});
Steven V
  • 16,357
  • 3
  • 63
  • 76
sheng
  • 1,226
  • 8
  • 17
  • And gtl.hig.no has to be your own domain, else you'll get 'Access-Control-Allow-Origin' errors in chrome. – Ruben-J Dec 03 '13 at 14:31
  • 1
    @Ruben-J good point, unless the website allows Cross-Origin Resource Sharing (CORS). – sheng Dec 03 '13 at 14:40