标题未设置 - SlimFramework [英] Header not set - SlimFramework

查看:18
本文介绍了标题未设置 - SlimFramework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 SlimFramework

I use SlimFramework

当我使用 xampp 运行我的本地脚本时,它工作正常.但是我将脚本上传到服务器,现在它解决了未设置标头的错误.

When i run my script locali with xampp it works fine. But i uploaded the script to the server and now it cone the error that the header was not set.

XHR 不允许 GET 请求的负载.或在设置中更改方法定义.

XHR does not allow payloads for GET request. or change a method definition in settings.

这里是 angular 的脚本

Here the script in angular

$rootScope.globals = $cookies.getObject('globals') || {};
    if ($rootScope.globals.currentUser) {
        $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.token;
    }

$rootScope.$on('$locationChangeStart', function (event, next, current) {
        var restrictedPage = $.inArray($location.path(), ['/login', '/register', '/password']) === -1;
        var loggedIn = $rootScope.globals.currentUser;
        if (restrictedPage) {
            if (!loggedIn) {
                $location.path('/login');
            } else {
                UserService.checkToken($rootScope.globals.currentUser.token)
                    .then(function (response) {
                        if (!response.success) {
                            $location.path('/login');
                        }
                    });

            }
        }
    });

<小时>

function checkToken(token) {
        return $http.get('api/v1/token').then(handleCallback, handleCallback);
    }
function handleCallback(res) {
        console.log(res);
        return res.data;
    }

这里是 SlimFramework 的脚本

And here the script with SlimFramework

$config['displayErrorDetails'] = true;
$config['addContentLengthHeader'] = false;
$config['determineRouteBeforeAppMiddleware'] = true;

$app = new \Slim\App(["settings" => $config]);
$container = $app->getContainer();

// This is the middleware
// It will add the Access-Control-Allow-Methods header to every request


$app->add(function ($req, $res, $next) {
    $response = $next($req, $res);
    return $response
        ->withHeader('Access-Control-Allow-Origin', '*')
        ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
        ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});


$app->get('/token', function ($request, $response){
    $token = $request->getHeaderLine('Authorization');
    if($token){
        $db = new DbOperation();
        if($db->checkAuthentication($token)){
            $return = $response->withJson(["success"=> true], 200);
        } else {
            $return = $response->withJson([
                "success"=> false,
                "message"=>'Invalid token'
            ], 403);
        }
    } else {
        $return = $response->withJson([
            "success"=> false,
            "message"=>'Header not set.'
        ], 403);
    }
    return $return;
});

我的问题是什么?大家都知道吗?

Whats my Problem? Everyone knows?

谢谢

更新:获取请求

API 测试的响应

HTTP/1.1 403 Forbidden
Server: nginx
Date: Mon, 27 Mar 2017 11:57:27 GMT
Content-Type: application/json;charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.6.30
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization
Access-Control-Allow-Methods: GET
X-Powered-By: PleskLin

推荐答案

如果不确定NG生成的header名称是什么,可以调试发送给SLIM的header.在 SLIM 中,可以这样做:

If you are not sure what is the header name generated by NG, you can debug the header sent to SLIM. In SLIM, it can be done like this:

$headers = $request->getHeaders();
foreach ($headers as $name => $values) {
    echo $name . ": " . implode(", ", $values);
}

我使用 jquery,我在全局标头中设置了令牌,如下所示:

Im using jquery, I set token in header globally, like this:

 $.ajaxPrefilter(function( options, oriOptions, jqXHR ) {
    jqXHR.setRequestHeader("Authorization", sessionStorage.token);
 }); 

这将发送一个带有标题名称的令牌:

That will send a token with a header name:

HTTP_AUTHORIZATION

获取特定的头变量:

   $token_array = $request->getHeader('HTTP_AUTHORIZATION');

   if (count($token_array) == 0) {
       $data = Array(
            "jwt_status" => "token_not_exist"
        );  

        return $response->withJson($data, 401)
                        ->withHeader('Content-type', 'application/json');                   
   }

    $token = $token_array[0];

这篇关于标题未设置 - SlimFramework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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