views:

309

answers:

1

I am trying to make a XMLHttpRequest Request to a content which is on local, runnin on local webserver from a HTML File and javascript residing locally.

it will hit the webserver request, and then even thou it sends the data back, in my javscript, at once I get readyState 4 and status as 0.

If i try putting the pages in the webserver foloder and access them as webpage slke http;//localhost/ filename then it works fine.

+1  A: 

This is to do with the fact that HTTP status codes are returned by web servers and, since you're accessing the local file system, a status code can't be returned -- hence Unknown (0). Some links:


Following your comments, it's a little clearer as to what is causing your problem. XMLHttpRequests are restricted in each browser by the Same origin policy. If you try to access a file on a different domain, port or protocol the request will return a status code of 0 and nothing for responseText. The most suitable workaround for the time being is JSON with Padding.

Andy E
I think I put the question in a wrong way.. Let me give more details:i have test.html in my c:\ drive and I have a local webserver runnin in qt, and i have some plugin written to that webserver which will get the request and send some response text "hello".in test.html i m making a xml xhr request which will make a GET request like http://localhost:8080/test which will return the text "hello" by that plugin.Now if I directly open test.html from C:\ it doesnt work, i mean i get response 4 and status 0, and response text nothing.. but the request is passing through webserver and plugin.
deepak
Also to mention, if I put test.html in the webserver's folder, and make arequest to that page like.. http://localhost:8080/test.html it works fine.
deepak
@deepak: I see what you mean now. I've updated my answer to reflect the problem you're experiencing.
Andy E