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

查看:92
本文介绍了未设置标题-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.

在此处以角度显示脚本

$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?

Thx

更新:
获取请求

UPDATE: Get request

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,您可以调试发送到SLIM的标头。在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

要获取特定的标头变量:

To get specific header variable:

   $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天全站免登陆