views:

65

answers:

1

I have the following code that works fine in IE:

<HTML>
<BODY>
<script language="JavaScript">
text="";
req = new XMLHttpRequest();
if (req) 
{
    req.onreadystatechange = processStateChange;
    req.open("GET", "http://www.boltbait.com", true);
    req.send();
}

function processStateChange()
{
    // is the data ready for use?
    if (req.readyState == 4) {
        // process my data
        alert(req.status);
        alert(req.responseText);
   }
}
</script>
</BODY>
</HTML>

In IE, the first alert returns 200, the second returns the web page.

However, in Chrome the first alert returns 0 and the second returns the empty string.

My intent is to grab a web page into a string for processing. If I'm not doing this right, how should I be doing this?

Thanks.

A: 

In general, due to the same origin policy (for security reasons), you can't make request to URLs outside your domain. So, if your domain isn't boltbait.com, you can't make that request. What's strange is that IE doesn't give you an error...

However, in Chrome, an extension can make cross-origin requests (check this).

Yassin
So, if this is true, how do I make a call to a web service to get weather data or stock quote info located on a server other than where my web page is located?
BoltBait
You can use JSONP (http://en.wikipedia.org/wiki/JSON#JSONP)
Yassin
BoltBait: You'll need support from the external site for that. If they provide such support, they'll probably have information about it on the site. If not, you're out of luck. The security is there to specifically prevent people fetching data from sites that haven't opted in to it.
Matti Virkkunen
OK, looks like I can't easily do what I was planning. Thanks, everyone!
BoltBait