我有一个无法理解的问题, 我试图使用Vuejs向Laravel发送axios发布请求, 正如他们所描述的那样:
我发送帖子请求时的问题
第一个完成但是如果我发送另一个它给了我
TokenMismatchException
请帮帮我
我的vue文件中的代码
login() {
this.userNotExiste = null;
var app = this
axios.post('/user/login', {email: this.email, password: this.password})
.then(function (response) {
if (response.data.userConnected) {
app.userConnected = response.data.userConnected;
setTimeout(function () {
window.location.href = window.location;
}, 1000);
}
})
.catch(function (error) {
if (error.response) {
if (!_.isEmpty(error.response.data))
if (!_.isEmpty(error.response.data.messages)) {
if (!_.isEmpty(error.response.data.messages.email)) app.loginErrors.email = error.response.data.messages.email[0]
if (!_.isEmpty(error.response.data.messages.password)) app.loginErrors.password = error.response.data.messages.password[0]
}
if (!_.isEmpty(error.response.data.userNotExiste)) app.userNotExiste = error.response.data.userNotExiste
} else if (error.request) {
console.log(error.request)
} else {
console.log('Contact : contact@33sprog.com')
}
})
}
在我的laravel部分
public function login(Request $request)
{
$validation = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required',
], [
'email.email' => 'Entrer adresse email correct',
'email.required' => 'Veuillez saisir une adresse email',
'password.required' => 'Veillez saisir votre mot de passe',
]);
if ($validation->fails()) {
return response()->json([
'messages' => $validation->messages(),
], 400);
}
$user = Auth::attempt(['email' => $request->email, 'password' => $request->password]);
if (!$user) {
return response()->json([
'userNotExiste' => 'Adresse email ou mot de passe incorrecte',
], 400);
}
return response()->json(['userConnected' => 'Vous etes bien connecte'], 200);
}
在我的php文件中
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
let token = document.head.querySelector('meta[name="csrf-token"]');
if (token) {
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content;
} else {
console.error('CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token');
}
感谢您的帮助