Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vue.js 如何强制axios GET请求发送标头?_Vue.js_Axios_Lumen - Fatal编程技术网

Vue.js 如何强制axios GET请求发送标头?

Vue.js 如何强制axios GET请求发送标头?,vue.js,axios,lumen,Vue.js,Axios,Lumen,尽管我的代码在Vue中的一个组件中,但问题在于Axios,让我解释一下原因。所以,我想得到一些信息,比如: axios.get('http://localhost:8181/roles/1', { headers: { 'Api-Token': 'tokenTOKEN', 'Content-Type': 'application/json' } } ) .then(response => {console.log(response)})

尽管我的代码在Vue中的一个组件中,但问题在于Axios,让我解释一下原因。所以,我想得到一些信息,比如:

axios.get('http://localhost:8181/roles/1',  
  {
    headers: {
      'Api-Token': 'tokenTOKEN',
      'Content-Type': 'application/json'
    }
  }
)
.then(response => {console.log(response)})
.catch(response => {
  console.log(response);
})
<?php

namespace App\Http\Middleware;

use Closure;

class JsonVerifier
{
    public function handle($request, Closure $next)
    {
        if($request->isJson())
        {
            return $response = $next($request);
        }
        else
        {
            return response('Unauthorized.', 401);
        }
    }
}
var miInit = { method: 'GET',
               headers: {
                 'Api-Token': 'tokenTOKEN',
                 'Content-Type': 'application/json'
             },
             mode: 'cors',
             cache: 'default' };

fetch('http://localhost:8181/roles/1',miInit)
.then(function(response) {
  console.log(response);
})
所以,是的,我正确地导入了Axios。是的,我知道我们不应该在GET请求中发送内容类型头。然而,我已经读过RFC7231,它并没有说不可能,只是不常见。因此,我们希望在我的请求中发送一个内容类型头

那么,我怎么知道它不起作用呢?我在Lumen API中的一个中间件是这样的:

axios.get('http://localhost:8181/roles/1',  
  {
    headers: {
      'Api-Token': 'tokenTOKEN',
      'Content-Type': 'application/json'
    }
  }
)
.then(response => {console.log(response)})
.catch(response => {
  console.log(response);
})
<?php

namespace App\Http\Middleware;

use Closure;

class JsonVerifier
{
    public function handle($request, Closure $next)
    {
        if($request->isJson())
        {
            return $response = $next($request);
        }
        else
        {
            return response('Unauthorized.', 401);
        }
    }
}
var miInit = { method: 'GET',
               headers: {
                 'Api-Token': 'tokenTOKEN',
                 'Content-Type': 'application/json'
             },
             mode: 'cors',
             cache: 'default' };

fetch('http://localhost:8181/roles/1',miInit)
.then(function(response) {
  console.log(response);
})
而且它有效!在这两种情况下(使用Postman和
fetch()
),我的API都会返回所需的数据

然而,当我尝试使用Axios时,我得到一个401响应,带有“Unauthorized”字样,这意味着Axios没有正确发送头

现在,问题来了。在axios GET请求中是否有其他发送头的方法?我如何才能强制Axios发送头文件,而不管
fetch()
和Postman的情况如何

Axios自动()删除
内容类型
标题,如果您发送的请求没有正文,就像任何
GET
请求一样


您可能正在查找
接受
头和
$request->wantsJson()
(或
acceptsJson()
)。它永远不会是JSON。您是否考虑过
接受
头和
$request->wantsJson()
函数,这实际上是用于此场景的功能?您应该能够使用类似于
获取
的方法@没有。Axios(正确地)在
内容类型
标题和
GET
或空
POST
等无数据请求的情况下防止了这种情况@切加约兹-啊,我看问题太快了-我认为这是剥离所有的标题。。。不过,对我来说,他们为什么要把它剥掉是完全有道理的!