jQuery Ajax request from local filesystem (Windows file:///)

前端 未结 3 1341

I\'m trying to do an ajax request to get the contents of \"http://localhost/\" running on Windows Wamp Server.

The script is running from something like

相关标签:
3条回答
  • 2020-11-27 07:05

    You say that the script is running from a file:/// URL. It's best not to do AJAX requests from file URLs, because they are treated inconsistently. Chrome, for example, entirely disallows them.

    However, your bigger problem here is the same-origin policy: you can only make AJAX requests to the same host as the web page itself. file:/// and http://localhost are not the same host (even if they are the same machine).

    It's best to run everything off http://localhost.

    0 讨论(0)
  • 2020-11-27 07:06

    This probably won't work, as the browser will think this is a cross-domain request. You've accessed the file via a file:// URL, but are trying to retrieve data from http://localhost. Try accessing your original file from http://localhost as well, and it'll probably start to work.

    0 讨论(0)
  • 2020-11-27 07:26

    Problem Solved!

    I just had to add this header to my index.php file for http://localhost/

    header('Access-Control-Allow-Origin: *');

    Thanks for your help anyhow guys!

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