在学习 ajax 时遇到了一个问题

XMLHttpRequest cannot load  file:xxxxxxxx . Cross origin requests are only supported for HTTP.  

谷歌了下, 在stackoverflow 上发现了原因所在:XMLHttpRequest 必须通过服务器打开,不能直接本地在文件目录下打开, 如下图:

利用 pyhon 解决 Cross Origin Requests

同时也介绍了几种解决方法,既然要通过服务器, 肯定要先开, apache什么的 有的自然最好, 但还有中更方便的方法, 通过 python 的 SimpleHTTPServer.

具体方法是: 通过控制台进入所需要打开的文件目录下, 然后执行命令 python -m SimpleHTTPServer 8000 , 在打开  http://localhost/,就会看到该目录下的子目录了. 

利用 pyhon 解决 Cross Origin Requests

如果http://localhost// 显示无法打开网页, 可能是端口被占用了, 换其他的试试。

相关文章:

  • 2022-12-23
  • 2021-08-10
  • 2022-12-23
  • 2021-12-23
  • 2021-10-15
  • 2021-06-16
  • 2021-12-16
猜你喜欢
  • 2021-04-06
  • 2021-09-14
  • 2022-12-23
  • 2023-03-20
  • 2021-11-26
  • 2021-08-31
相关资源
相似解决方案