利用 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// 顯示沒法打開網頁, 多是端口被佔用了, 換其餘的試試。服務器

相關文章
相關標籤/搜索