views:

87

answers:

1

EDIT: It's been pointed out below that this doesn't work because craigslist doesn't set an Allow-Cross-Domain header. OK, I'll buy that. Is there any other way to use javascript in firefox to download a page cross-domain then?


Yes, I know the following code does not work in IE. I know IE expects me to use XDomainRequest() instead. I don't care about that. This is firefox only.

I'm trying to do a cross-domain web request in firefox javascript. I keep getting a status of 0. Does anyone know why?

var url = "http://newyork.craigslist.org";
var xdr = new XMLHttpRequest(); //Yes, I know IE expects XDomainRequest. Don't care
xdr.onreadystatechange = function() { 
  if (xdr.readyState == 4) {
      alert(xdr.status); //Always returns 0! And xdr.responseText is blank too
  }
}
xdr.open("get", url, true);
xdr.send(null);

Shouldn't that work?

A: 

Craigslist doesn't allow cross-domain requests to it. It needs to send a proper Access-Control-Allow-Origin header.

Eli Grey
So is there any other way I can have javascript request a webpage then?
Tav
One way or the other, you must have third-party co-operation to do cross-domain XMLHttpRequests. Otherwise it'd be an enormous security hole, wouldn't it? The usual way it is done is with JSONP, but if CraigsList have not deliberately provided you with an API you can access from other domains, it's not going to happen. You would have to run a proxy on your own server to make the request.
bobince