gpt4 book ai didi

jquery - Vue 和 Vue 资源

转载 作者:搜寻专家 更新时间:2023-10-30 22:09:15 24 4
gpt4 key购买 nike

我正在尝试通过 vue-resource 发送发布请求,但我似乎无法弄清楚我做错了什么。

我正在使用 Laravel 5.1 来处理请求。

以下 jquery 代码在我的 Vue 方法中运行良好。

 $.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.');
}
}
});

但是,由于某些原因,将上面的 jquery 替换为以下 vue-resource post 请求不起作用。我确定这很简单,但我似乎无法弄清楚。 Vue-resource 已正确包含在项目中,因为我正在使用它毫无问题地获取请求。

this.$http.post('/purchase/save-cart-items', {purchaseItems:purchaseItems}, function (data, status, request) {
alert("Cart saved successfully.");
}).error(function (data, status, request) {
alert('There was a problem saving this cart. Please try again.');
});

最佳答案

您可能缺少 csrf token :

html

<meta id="token" name="token" content="{{ csrf_token() }}">

js

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');

关于jquery - Vue 和 Vue 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34322553/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com