XMLHttpRequest无法加载url。在预检响应中,Access-Control-Allow-Methods不允许使用方法PUT

时间:2016-01-25 08:48:20

标签: javascript angularjs ajax

我正在使用angularjs $http来获取ajax请求。它适用于getpost,但在put的情况下会出错。

响应标题:

Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Accept-Language, Host, Referer, User-Agent, X-API-KEY
Access-Control-Allow-Methods:OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1728000
Content-Length:0
Date:Mon, 25 Jan 2016 08:44:05 GMT
Server:spray-can/1.3.2

请求标题:

Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,hi;q=0.6
Access-Control-Request-Headers:accept, content-type, x-api-key
Access-Control-Request-Method:PUT
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:8000
Referer:http://localhost:8000/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36

1 个答案:

答案 0 :(得分:1)

尝试设置以下标题:

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: Origin, Content-Type, Authorization, X-Auth-Token');
header('Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS');