2014-09-25 79 views
2

我在製作跨站點ajax調用時遇到問題。使用nginx,我相信我已經在服務器配置中添加了正確的頭文件,但它仍然不能在我的JS代碼或控制檯中工作。我想知道我做錯了什麼。即使使用Access-Control-Allow-Origin標頭,XMLHttpRequest CORS請求失敗

這是我鍵入到控制檯的響應是我們熟悉的「否‘訪問控制允許來源’標頭存在」錯誤:

$.get("//www.example.com"); 
Object {readyState: 1, getResponseHeader: function, getAllResponseHeaders: function, setRequestHeader: function, overrideMimeType: function…} 
XMLHttpRequest cannot load http://www.example.com/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://dictionary.aherriot.com' is therefore not allowed access. 

當我看從響應頭我初始網頁加載,我看到下面的標題:

Access-Control-Allow-Credentials:true 
Access-Control-Allow-Headers:X-Requested-With,Accept,Content-Type, Origin 
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, DELETE 
Access-Control-Allow-Origin:* 

,這裏是站點的nginx的配置文件:

server { 
    listen 80; 

    root /home/aherriot/sites/dictionary; 
    index index.html index.htm; 

    server_name dictionary.aherriot.com; 

    location/{ 

      add_header 'Access-Control-Allow-Origin' '*'; 
      add_header 'Access-Control-Allow-Credentials' 'true'; 
      add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE'; 
      add_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin'; 

    } 
} 

我很難過,因爲我失蹤了。我還需要做什麼來允許CORS?謝謝。

回答

0

與問題的問題是存在的CORS是什麼根本性的誤解。原始服務器不指定頁面可以通過XMLHttpRequest請求訪問哪些第三方域,而是指定哪些域可以連接到它的外部域上的服務器。