1. 程式人生 > >ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)

ajax跨域(No 'Access-Control-Allow-Origin' header is present on the requested resource)

一、跨域問題:

eg:在A網站中,我們希望使用Ajax來獲得B網站中的特定內容。如果A網站與B網站不在同一個域中,那麼就出現了跨域訪問問題。你可以理解為兩個域名之間不能跨過域名來發送請求或者請求資料,否則就是不安全的。跨域訪問違反了同源策略。
總而言之,同源策略規定,瀏覽器的ajax只能訪問跟它的HTML頁面同源(相同域名或IP)的資源。

二、解決方案:

1.Get:

加上:dataType:'jsonp'

2.Post:

(1)web.config中加:

<httpProtocol>
    <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
</httpProtocol>
3.參考文章:

(1)http://blog.csdn.net/zhoucheng05_13/article/details/53580683

(2)https://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource