我正在努力应对以下要求:
var spice = $http.put("http://localhost:8000/api/v1/Spices/", sanitizeSpice(spice, id));
其中sanitizeSpice返回类似{amount:“54”,id:“2”)
的内容我收到以下错误:
XMLHttpRequest cannot load http://localhost:8000/api/v1/Spices/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我在Laravel中设置了以下过滤器(如我在此处找到的其他示例中所示):
App::before(function($request)
{
if (Request::getMethod() == "OPTIONS") {
$headers = array(
'Access-Control-Allow-Origin', 'http://localhost',
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Headers'=> 'X-Requested-With, content-type',);
return Response::make('', 200, $headers);
}
});
App::after(function($request, $response)
{
$response->header('Access-Control-Allow-Origin', '*');
$response->header('Access-Control-Allow-Methods', 'POST,GET,DELETE,PUT,OPTIONS');
});
我做错了什么?我没有得到POST,GET或DELETE的错误。提前谢谢!
答案 0 :(得分:0)
如果你谷歌访问控制 - 允许 - 来源,第一次点击是https://developer.mozilla.org/en/docs/HTTP/Access_control_CORS
您需要在nginx或apache2配置中允许ajax调用。
使用谷歌查找两者的良好配置。 例如。对于nginx:
add_header 'Access-Control-Allow-Origin' '*';