laravel 预检响应无效(重定向)错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35841786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
response for preflight is invalid (redirect) error
提问by Abdulla
I am new to Laravel and Lumen framework. I am doing my first project using Lumen. I am trying to create an API calling from angular
我是 Laravel 和 Lumen 框架的新手。我正在使用 Lumen 做我的第一个项目。我正在尝试从 angular 创建一个 API 调用
Here is my angular code :
这是我的角度代码:
app.controller('ListCtrl', ['$scope', '$http', '$location', '$window', function($scope, $http, $location, $window) {
$scope.data = {};
$scope.getdata = function() {
$scope.datas = [];
$headers = {
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS, PUT',
'Content-Type': 'application/json',
'Accept': 'application/json'
};
$http({
url: "http://localhost/service/public/getdata/",
method: "GET",
params: {'place':$scope.data.place,'pincode':$scope.data.pincode},
headers: $headers
})
.success(function(data,status,headers,config) {
$scope.datas=JSON.stringify(data);
console.log($scope.datas);
$scope.navig('/show.html');
})
.error(function(){
alert("failed");
});
};
$scope.navig = function(url) {
$window.location.href = url;
};
}]);
And here is my Lumen route.php :
这是我的 Lumen route.php :
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
$app->get('/', function () use ($app) {
return $app->version();
});
$app->get('getdata','App\Http\Controllers\PlaceController@index');
And here is PlaceController.php
这是PlaceController.php
<?php
namespace App\Http\Controllers;
use App\Places;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class PlaceController extends Controller
{
public function __construct()
{
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type");
//header("Access-Control-Allow-Origin: http://localhost:8100");
}
public function index()
{
$places = Place::all();
return response()->json($places);
}
}
But it shows "XMLHttpRequest cannot load http://localhost/service/public/getdata/?place=sdfs. Response for preflight is invalid (redirect)" error in console.log.
但它在 console.log 中显示“XMLHttpRequest 无法加载http://localhost/service/public/getdata/?place=sdfs。预检响应无效(重定向)”错误。
I have googled for two days,but cant find a solution.
我已经谷歌搜索了两天,但找不到解决方案。
Please help
请帮忙
回答by André Godoy
You might be having problems due to invalid/incorrect Headers in your request. The only type of header that PlaceController
seems to allow is Content-Type
, but you're sending more than that.
由于请求中的标题无效/不正确,您可能会遇到问题。PlaceController
似乎允许的唯一标题类型是Content-Type
,但您发送的内容不止于此。
Also, Access-Control-Allow-Origin
and Access-Control-Allow-Methods
headers should be added to the server response for your request, not to the request itself.
此外,Access-Control-Allow-Origin
和Access-Control-Allow-Methods
头应添加到您的请求的服务器响应,而不是请求本身。
From MDN, cross-site requests (which seems to be your case) have to meet the following conditions:
从MDN,跨站点请求(这似乎是你的情况)必须满足以下条件:
- The only allowed methods are:
- GET
- HEAD
- POST
- Apart from the headers set automatically by the user agent (e.g. Connection, User-Agent, etc.), the only headers which are allowed to be manually set are:
- Accept
- Accept-Language
- Content-Language
- Content-Type
- The only allowed values for the Content-Type header are:
- application/x-www-form-urlencoded
- multipart/form-data
- text/plain
- 唯一允许的方法是:
- 得到
- 头
- 邮政
- 除了由用户代理自动设置的标头(例如 Connection、User-Agent 等)外,唯一允许手动设置的标头是:
- 接受
- 接受语言
- 内容语言
- 内容类型
- Content-Type 标头的唯一允许值是:
- 应用程序/x-www-form-urlencoded
- 多部分/表单数据
- 文本/普通
Note: I never worked with Laravel or Lumen, but in my case if I don't set the headers correctly I end up with the same response for preflight is invalid (redirect)
error.
注意:我从未使用过 Laravel 或 Lumen,但在我的情况下,如果我没有正确设置标题,我最终会遇到相同的response for preflight is invalid (redirect)
错误。