Ajax not working when i read a local file

前端 未结 2 1958
暗喜
暗喜 2021-01-25 08:49

Okay i cannot solve this problem for 2 days straight.. This examlpe here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_xml2 works just fine on the w3schools site. B

相关标签:
2条回答
  • 2021-01-25 09:07

    The file can be coincidentally located on the same machine, but can't be accessed by the file system. It has to be accessed by a web server running on that machine.

    So, for example, if you have a web server which serves files from:

    c:\server\web\
    

    Then your AJAX requests for the file wouldn't be to this:

    c:\server\web\somefile.xml
    

    It would be this:

    http://localhost/somefile.xml
    

    The file is "local" in the sense that it's on the same computer. But it's not "local" in the sense that it's coming from a URL and not from a file system path. For fairly obvious security reasons, browsers don't allow JavaScript code to access the file system. (Imagine a world where any website could read any file on your computer without asking.)

    Both the HTML page which contains the JavaScript code as well as the file being read by the JavaScript code should be served from the web server, not opened from the file system.

    0 讨论(0)
  • 2021-01-25 09:12

    You can't work with local files. The local file system is sandboxed so that, for example, you can't double click an HTML document in an email attachment, open it in your browser and have it upload your private files to the sender's server.

    If you want to test Ajax locally, then install a local webserver and test through it.

    0 讨论(0)
提交回复
热议问题