预检的响应无效(重定向)错误 [英] response for preflight is invalid (redirect) error

查看:846
本文介绍了预检的响应无效(重定向)错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel和Lumen框架的新手。我正在使用流明做我的第一个项目。我正在尝试创建一个从角度调用的API

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

这是我的角度代码:

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;
    };
}]);

这是我的流明 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');

这里是 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);
    }
}

但它显示XMLHttpRequest无法加载 http:// localhost / service / public / getdata /?place = sdfs 。预检的响应无效(重定向)console.log中的错误。

But it shows "XMLHttpRequest cannot load http://localhost/service/public/getdata/?place=sdfs. Response for preflight is invalid (redirect)" error in console.log.

我用Google搜索了两天,但无法找到解决方案。

I have googled for two days,but cant find a solution.

请帮助

推荐答案

由于请求中的标头无效/不正确,您可能会遇到问题。 PlaceController 似乎允许的唯一类型的标头是 Content-Type ,但是你发送的不仅仅是那个。

You might be having problems due to invalid/incorrect Headers in your request. The only type of header that PlaceControllerseems to allow is Content-Type, but you're sending more than that.

此外, Access-Control-Allow-Origin Access-Control-Allow-方法标题应该添加到您的请求的服务器响应中,而不是添加到请求本身。

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.

来自 MDN ,跨站点请求(似乎是您的情况)必须满足以下条件:

From MDN, cross-site requests (which seems to be your case) have to meet the following conditions:



  • 唯一允许的方法是:


    • GET

    • HEAD

    • POST


  • 接受

  • 接受语言

  • Content-L anguage

  • 内容类型

  • Accept
  • Accept-Language
  • Content-Language
  • Content-Type

  • application / x-www-form-urlencoded

  • multipart / form-data

  • text / plain

注意:我从未使用Laravel或Lumen,但在我的情况下如果我没有正确设置标题我最终得到相同的预检的响应无效(重定向) 错误。

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.

这篇关于预检的响应无效(重定向)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆