我有2个项目,其中laravel基本代码相同但在一个项目中vue.js部分更高级。 现在我在javascript控制台中有一条奇怪的错误消息。
CSRF token not found: https://laravel.com/docs/csrf#csrf-x-csrf-token
在我的下一步中,我复制了旧版本的功能vue代码但是同样的错误。 我复制的代码在另一个项目中工作
引导代码是相同的,如下所示:
window._ = require('lodash');
/**
* We'll load jQuery and the Bootstrap jQuery plugin which provides support
* for JavaScript based Bootstrap features such as modals and tabs. This
* code may be modified to fit the specific needs of your application.
*/
try {
window.$ = window.jQuery = require('jquery');
require('bootstrap-sass');
} catch (e) {}
/**
* We'll load the axios HTTP library which allows us to easily issue requests
* to our Laravel back-end. This library automatically handles sending the
* CSRF token as a header based on the value of the "XSRF" token cookie.
*/
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
/**
* Next we will register the CSRF Token as a common header with Axios so that
* all outgoing HTTP requests automatically have it attached. This is just
* a simple convenience so we don't have to attach every token manually.
*/
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');
}
我在bootstrap.js代码中做了一些测试,如下所示:
console.log(token);
结果是空的。
我能做什么?
答案 0 :(得分:0)
您必须先在html听众的meta
标记中进行设置:
<meta name="csrf-token" content="{{ csrf_token() }}" />
答案 1 :(得分:0)
尝试使用window.token
代替token