Ajax請求:本地跨域的問題

來源:互聯網
上載者:User

標籤:support   用戶端   ima   編輯   不能   src   protoc   host   control   

問題出現一:

1.Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

釋:請求源僅支援http、data、chrome、chrome-extension、https協議。

出現此問題是我將ajax請求的url寫成了以文本協議訪問,換句話說就是,涉及到 AJAX 操作的頁面“不能”使用檔案協議訪問(檔案的方式訪問)。

 

根據網路上的解決方案:設定主流瀏覽器的請求參數或是在sublime編輯器中安裝外掛程式,我都使用過了,還是沒有用。

後來查資料,涉及到了一個同源策略的概念,大概瞭解一下。

我將請求的url改成以http協議訪問,即:xhr.open(‘GET‘,‘http://localhost/Ajax/request-url.php‘)。

然後重新訪問,然而。。。。。

出現問題二:

2. No ‘Access-Control-Allow-Origin‘ header is present on the requested resource. Origin ‘null‘ is therefore not allowed access.

釋:在請求源裡沒有允許控制源的請求,即在伺服器響應用戶端的時候,帶上Access-Control-Allow-Origin頭資訊。

所以,我在請求源的頭部加入這行代碼:header("Access-Control-Allow-Origin: *");

然後繼續訪問,duang~ 問題解決啦。

Ajax請求:本地跨域的問題

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.