Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue and Vue Resource

Tags:

jquery

vue.js

I'm trying to send a post request via vue-resource and I can't quite seem to figure out what I'm doing incorrectly.

I'm using Laravel 5.1 to process the request.

The following jquery code works fine from within my Vue method.

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

However, the replacing the jquery above with the following vue-resource post request doesn't work for some reason. I'm sure it's something simple, but I can't seem to figure it out. Vue-resource is properly included in the project as I'm using it for get requests without issue.

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.');
});
like image 263
Joshua Avatar asked Dec 16 '15 21:12

Joshua


People also ask

What is Vue-resource?

The plugin for Vue. js provides services for making web requests and handle responses using a XMLHttpRequest or JSONP.

What is the use of Vue-resource?

vue-resource is a library for Vue. js that provides an API for sending Ajax requests by wraping the JavaScript's XMLHttpRequest interface or by using JSONP. vue-resource has many features such as: the support of the Promise API and URI Templates.

Is Vue-resource deprecated?

Does this mean vue-resource is deprecated? No. It's just no longer part of the “official recommendation”. The repo will be moved back to pagekit/vue-resource, and will continue to work.

What is difference between Vue and VueJS?

Vue Native is a mobile framework to build truly native mobile app using Vue. js. Its is designed to connect React Native and Vue. js Vue Native is a wrapper around React Native APIs, which allows you to use Vue.


2 Answers

You may be missing the csrf token:

html

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

js

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');
like image 151
Douglas.Sesar Avatar answered Oct 07 '22 17:10

Douglas.Sesar


Vue's author has stopped updating and maintaining vue-resource. He himself also suggested using axios, axios more easy to use and easy to maintain. axios introduction

If you just started learning Vue, here's an entry-level demo. Although it is only a small application, but it covers a lot of knowledge points (vue2.0 + vue-cli + vue-router + vuex + axios + mysql + express + pm2 + webpack), including front-end, back-end, database and other sites Some of the necessary elements, for me, learning great significance, would like to encourage each other!

Github Demo

like image 43
AlloyTeamZy Avatar answered Oct 07 '22 16:10

AlloyTeamZy