Why does this xmlHttpRequest fail?

http://jsfiddle.net/FarqA/

ajax_info.txt is a valid file on my computer. The error is returned like this:

 Uncaught Error: INVALID_STATE_ERR DOM Exception 11 
+1
source share
2 answers

Many browsers will refuse to download local files using XMLHttpRequest as a security measure. AJAX requests are limited to the same origin policy , but, as a link to the Wikipedia page,

The behavior of checks with the same origin and related mechanisms is not well defined in a number of cases, for example, for protocols that do not have a clearly defined host or port name associated with their URLs (file :, data: etc. d.).

Downloading a local file, even with a relative URL, is the same as downloading a file using the file: protocol. Many web browsers limit this, not in vain - imagine that a malicious HTML file is running on your local computer that can download any file to your computer and send its contents to a remote server.

Therefore, I assume that the problem is that you are trying to upload a local file. Try serving your script on a local or remote web server and see if this fixes the problem. (If you have Python installed, you can go to the appropriate directory and run python -m SimpleHTTPServer 8000 , and then go to http: // localhost: 8000 / in your browser).

+1
source

In your script, each call to HTTPRequest() creates a new request object, so a request with the onReadystateChange handler onReadystateChange never sent, and the request that is sent does not have a handler function. Here is an updated version of your fiddle that should work: http://jsfiddle.net/HwYUS/

0
source

All Articles