利用 pyhon 解决 Cross Origin Requests

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

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

谷歌了下, 在stackoverflow 上发现了缘由所在:XMLHttpRequest 必须经过服务器打开,不能直接本地在文件目录下打开, 以下图:python

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

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

若是http://localhost// 显示没法打开网页, 多是端口被占用了, 换其余的试试。服务器

相关文章
相关标签/搜索