我正试着把数据从一个网页‘上传’到我运行在本地主机上的Laravel 5服务器上,这个网页也是本地主机服务的,但是来自其他服务器软件(nodejs)。但是我得到了这个错误:
XMLHttpRequest cannot load http://localhost:8888/data/. Method PUT is not allowed by Access-Control-Allow-Methods.创建put请求的代码是angularjs,如下所示:
return $http.put("http://localhost:8888/data/", { obj1: a, obj2: b });绕过CORS最简单的方法是什么?安全性不是问题。使用chrome中的postman插件,我可以发送put请求。
发布于 2015-04-24 22:29:08
这是我用于cors的laravel设置。
我已经创建了一个具有以下设置的http.php配置文件:
return [
'cors' => [
'origin' => '*',
'methods' => 'OPTIONS, GET, POST, PUT, PATCH, DELETE',
'headers' => 'X-Requested-With, Content-Type, Authentication-Token',
],
];在应用程序启动时在某个地方运行以下代码:
if ( ! App::runningInConsole()) {
header('Access-Control-Allow-Origin: ' . Config::get('http.cors.origin'));
header('Access-Control-Allow-Credentials: true'); // to be honest i've never used this option, but it looks like you'll need it
// handle preflight requests
App::before(function() {
if (Request::getMethod() === 'OPTIONS') {
return new Response('', 200, [
'Access-Control-Allow-Methods' => Config::get('http.cors.methods'),
'Access-Control-Allow-Headers' => Config::get('http.cors.headers'),
]);
}
});
}发布于 2015-04-24 22:23:19
邮递员不使用CORS。
要启用CORS (在客户端),请在put选项中添加:{ withCredentials: true }:
return $http.put("http://localhost:8888/data/", { obj1: a, obj2: b }, { withCredentials: true });注意:取决于您的服务器应用程序,您也可以在服务器端启用它们。
发布于 2015-04-24 22:25:37
这似乎不是一个角度问题。您应该尝试使用以下命令在PHP上启用CORS:
// Enable CORS
// In production, replace * with http://yourdomain.com
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');https://stackoverflow.com/questions/29849966
复制相似问题