我正试图通过vue-resource发送一个帖子请求,我似乎无法弄清楚我做错了什么.
我正在使用Laravel 5.1来处理请求.
$.ajax({ type: 'POST',url: '/purchase/save-cart-items',data: { 'purchaseItems' : purchaseItems},success: function (response) { if(response == "ok") { alert("Cart saved successfully."); } else { alert('There was a problem saving this cart. Please try again.'); } } });
但是,使用以下vue-resource post请求替换上面的jquery由于某种原因不起作用.我确信这很简单,但我似乎无法弄明白. Vue-resource正确地包含在项目中,因为我正在使用它来获取请求而没有问题.
this.$http.post('/purchase/save-cart-items',{purchaseItems:purchaseItems},function (data,status,request) { alert("Cart saved successfully."); }).error(function (data,request) { alert('There was a problem saving this cart. Please try again.'); });
解决方法
您可能缺少csrf令牌:
HTML
<Meta id="token" name="token" content="{{ csrf_token() }}">
JS
Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');