2017-04-21 68 views
3

我有一個PHP API來驗證指定的憑據。在我的PHP文件的開頭我有這個訪問控制允許起源 - 不工作

header('Access-Control-Allow-Origin: http://example.org'); 
header('Access-Control-Max-Age: 3628800'); 
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE'); 
header('Content-type: application/json'); 

雖然當我用我的API(使用AJAX)我得到這個錯誤:

XMLHttpRequest cannot load http://api.example.com/?params=parameters+go+here. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.org' is therefore not allowed access. 

注:我有理由相信,有在AJAX

$.ajax({ 
    url: 'http://api.example.com/?type=verify&username=' + username + '&password=' + password, 
    dataType: 'json', 
    success: function(result) { 
     success = result['success']; 
     return success; 
    } 
}); 

任何幫助一個缺陷,你可以提供將不勝感激。

感謝, CSF

+0

清楚你的頭是不是** **發送 - 驗證在瀏覽器** **開發工具網絡選項卡,看看有什麼響應頭實際發送 –

+0

@JaromandaX謝謝。剛剛檢查了開發者網絡選項卡,但我不知道如何查看標題。 – csf30816

+0

通過點擊請求(因爲我甚至不知道你有哪個瀏覽器,這是我能建議的最好的) –

回答

0

很容易......如果你使用Chrome我建議你安裝Allow-Control-Allow-Origin: *拓...

enter image description here

添加到Chrome瀏覽器後,您只需要啓用/禁用cross-origin resource sharing切換按鈕...

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?utm_source=chrome-app-launcher-info-dialog

+0

是的,但我想要的是這是一個任何人都可以訪問的API。 – csf30816

+0

請參閱[此鏈接](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Access-Control-Allow-Headers)... Access-Control-Allow-Headers不允許*作爲可接受的值... 您應該發送接受的標題(第一個X-Requested-With,如錯誤所述)。 –

+0

感謝您的鏈接 – csf30816

0

好吧,如果你只是想測試你的代碼,然後下載「允許控制允許原產地」擴展鏈接〜here ,但如果你希望它在每一個瀏覽器的工作/客戶沒有任何擴展的要求..那麼請參考here ..好運!

+0

有沒有使用PHP的版本?這將是非常感謝。 – csf30816

+0

參考這個 - http://www.mjhall.org/php-cross-origin-resource-sharing/ – Ankit

相關問題