当我使用RESTful API检索或发布数据时。在Angular中我使用了Restangular。我得到了" Request头字段Access-Control-Allow-Origin在预检响应中不允许使用Access-Control-Allow-Origin"浏览器中的错误消息。
RestangularProvider.setDefaultHeaders({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With',
'Content-Type': 'application/json',
'X-Requested-With' : 'XMLHttpRequest'
})
请求标题:
OPTIONS /api/membership HTTP/1.1
Host: localhost
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:8000
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.36
Access-Control-Request-Headers: access-control-allow-headers,access-control-allow-methods,access-control-allow-origin,x-requested-with
Accept: */*
Referer: http://localhost:8000/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
响应标题:
HTTP/1.1 200 OK
Date: Fri, 12 May 2017 11:17:49 GMT
Server: Apache/2.4.23 (Unix) OpenSSL/1.0.2j PHP/7.0.9 mod_perl/2.0.8-dev Perl/v5.16.3
X-Powered-By: PHP/7.0.9
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: *
Content-Length: 154
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json