Vuejs2 Laravel报告我的VueJS+JWT驱动系统中存在400(错误请求;未提供令牌)
当我通过axios从Vue组件提交表单时,会返回400个错误请求。通过登录/注册生成令牌工作正常,请求头显示授权头中存在的令牌。我已经检查了“handler.php”中异常报告的顺序,触发的JWTException是最后一个检查的异常类型。我读到我的.htaccess可能配置不正确,但事实并非如此 我在Windows机器上工作,除此之外,我不确定还能提供什么其他信息。下面是api.php的内容,下面是处理表单内容的控制器的内容,与现在的情况不同!,提交表单时使用的Vue方法,以及我的JWT.php内容: 路由::apiResource'/question','QuestionController'; 路由::apiResource'/category','CategoryController'; 路由::apirource'/question/{question}/reply','ReplyController'; 路由::post'/like/{reply}','LikeController@likeIt'; 路由::删除'/like/{reply}','LikeController@unLikeIt'; 路由::组[ “中间件”=>“api”, '前缀'=>'验证' ],功能$router{ 路由::post'login','AuthController@login'; 路线::发布“注销”后AuthController@logout'; 路线::张贴“注册”信息AuthController@signup'; 路由::后“刷新”,后“刷新”AuthController@refresh'; 路线:发布“我”和“我”AuthController@me'; 路由::post“有效载荷”和AuthController@payload'; QuestionController.php 名称空间App\Http\Controllers; 使用App\Models\Question; 使用\Http\Request; 使用Symfony\Component\HttpFoundation\Response; 使用App\Http\Resources\QuestionResource; 使用App\Http\Requests\QuestionRequest; 类控制器扩展控制器 { 公共函数构造 { $this->middleware'JWT',['except'=>['index','show']; //没有令牌,任何人都无法创建问题 } 公共函数storeRequest$request { $request['slug']=str_slug$request->title; 验证->用户->问题->创建$request->全部; 返回响应“已创建”,响应::HTTP\u已创建; } 创建.vue : 方法:{ 创造{ axios.post'/api/question',this.form .thenres=>console.logres.data .catcherror=>this.errors=error.response.data.error }... 我的JWT.php内容包括:Vuejs2 Laravel报告我的VueJS+JWT驱动系统中存在400(错误请求;未提供令牌),vuejs2,laravel-5.2,jwt,Vuejs2,Laravel 5.2,Jwt,当我通过axios从Vue组件提交表单时,会返回400个错误请求。通过登录/注册生成令牌工作正常,请求头显示授权头中存在的令牌。我已经检查了“handler.php”中异常报告的顺序,触发的JWTException是最后一个检查的异常类型。我读到我的.htaccess可能配置不正确,但事实并非如此 我在Windows机器上工作,除此之外,我不确定还能提供什么其他信息。下面是api.php的内容,下面是处理表单内容的控制器的内容,与现在的情况不同!,提交表单时使用的Vue方法,以及我的JWT.ph
namespace App\Http\Middleware;
use Closure;
use JWTAuth;
class JWT
{
public function handle($request, Closure $next)
{
JWTAuth::parseToken()->authenticate();
//JWTAuth::toUser(JWTAuth::getToken());
return $next($request);
}
}
我还能在哪里寻找我犯的错误呢
谢谢
Tom问题在于我一直在将app.js文件中的授权头设置为
const JWTtoken = `Bearer ${localStorage.getItem('token')}`;
window.axios.defaults.headers.common['Authorization'] = JWTtoken;
但是“承载人”已经/正在从发送的标头中剥离。因此,我更改了表单内容发送到服务器的方法,以便在该点添加授权标头:
create(){
const JWTtoken = `Bearer ${localStorage.getItem('token')}`;
window.axios.defaults.headers.common['Authorization'] = JWTtoken;
axios.post('/api/question',this.form)
.then(res => console.log(res.data))
.catch(error => this.errors = error.response.data.error)
}
现在一切都正常了
在这个问题上只花了三天时间