Kam*_*azz 5 ajax vue.js axios laravel-5.5
在配置X-CSRF字段并且我的所有代码都很简单之后,我正在尝试使用axios和最后一个Laravel版本5.5发出一些请求:
axios.post('/post-contact',{name:'Kamal Abounaim'})
.then((response)=>{
console.log(response)
}).catch((error)=>{
console.log(error.response.data)
})
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:419(未知状态)问题应该是什么感谢您的回答
Abd*_*bab 10
这是因为csrf-token发生的.只需添加带有csrf-token的元标记,然后将该标记<head>添加到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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10139 次 |
| 最近记录: |