背景:服務器提供https的api給浏覽器ajax調用,並要允許跨域訪問:
1. httpd.conf
去掉注釋LoadModule headers_module modules/mod_headers.so
2. 修改以下配置
conf/extra/httpd-ssl
<VirtualHost>
...
Header add Access-Control-Allow-Origin *
Header add Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header add Access-Control-Allow-Headers "Content-Type"
</VirtualHost>
這裡一定要加Header add Access-Control-Allow-Headers "Content-Type"。
猜測是因為客戶端要發送的類型是application/json,
所以在發送的OPTIONS請求頭裡出現:
Access-Control-Request-Headers:Content-Type, Accept
3. 重啟apache
參見:http://www.w3.org/TR/cors/