ajax – 在Laravel 5.5上使用axios发布请求
发布时间:2020-12-16 02:54:08 所属栏目:百科 来源:网络整理
导读:我正在尝试使用axios和最后一个Laravel版本5.5提出一些请求 配置X-CSRF字段后全部 我的代码很简单: axios.post('/post-contact',{name:'Kamal Abounaim'}) .then((response)={ console.log(response) }).catch((error)={ console.log(error.response.data)
我正在尝试使用axios和最后一个Laravel版本5.5提出一些请求
配置X-CSRF字段后全部 我的代码很简单: axios.post('/post-contact',{name:'Kamal Abounaim'}) .then((response)=>{ console.log(response) }).catch((error)=>{ console.log(error.response.data) }) 但我得到这个错误:419(未知状态) 解决方法
这是因为csrf-token发生的.只需在< head>中添加带有csrf-token的元标记.并将该标记添加到axios标头中.
// in the <head> <meta name="csrf-token" content="{{ csrf_token() }}"> <script type="text/javascript"> // For adding the token to axios header (add this only one time). var token = document.head.querySelector('meta[name="csrf-token"]'); window.axios.defaults.headers.common['X-CSRF-TOKEN'] = token.content; // send contact form data. axios.post('/post-contact',{name:'Kamal Abounaim' }).then((response)=>{ console.log(response) }).catch((error)=>{ console.log(error.response.data) }); </script> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |