Issues with AJAX integration within Vue.js 2

I am experimenting with using AJAX in conjunction with Vue.js 2:

let remoteUrl = '...................';

var app = new Vue({
    el: '#app',
    data: {
        items: []
    },
    created: function () {
        this.getFilms();
    },
    methods: {
        getFilms: function () {
            $.ajax({
                type: "GET",
                url: remoteUrl
            }).done(function (res) {
                console.log(res);
                self.items = res;
            }).fail(function (err) {
                alert("ERROR: " + err);
            });
        }
    }

this is the html:

            <table>
                <tbody>
                    <tr v-for="item in items">
                        <td>{{item.name}}</td>
                        <td>{{item.date}}</td>
                        <td>{{item.size}}</td>
                        <td>{{item.extension}}</td>
                    </tr>
                </tbody>
            </table>
        </div>

An output is visible in the console, however, the table remains empty. No errors are observed in the AJAX call. I have attempted both the created and mounted approaches. Why is the table not populating?

Answer №1

To resolve the issue, simply include the statement let self = {} immediately after defining your remoteUrl, unless you have already defined self elsewhere in your code.

This solution should work effectively if properties such as nome, data, size, and ext belong to your res object.

Answer №2

It's puzzling why some still feel the need to mix jquery with Vue. If you're using Vue, stick to writing in Vue. And if you prefer ng2/1, stay true to ng2/1.x

Let's face facts: the era of jquery is fading fast. With ng5 on the horizon and rumors of ng9 in 2018, it's clear that the days of coding in jquery are numbered; it'll be all about jquery only -OR- angular only -OR- Vue.js only.

Sure, you can keep patching together jquery+ng or jquery+Vue for now, but it's not a wise choice.

My recommendation? Let go of jquery to stay current. Join the conversation: Vue.js and jQuery?

Jquery: put it out to pasture. The sooner you abandon a sinking ship, the quicker you can set sail towards more promising opportunities.

Answer №3

After replacing jQuery with vue-resource, everything is now functioning perfectly! Thank you for the help!

let remoteUrl = '..............';

var app = new Vue({
    el: '#app',
    data: {
        items: []
    },
    created: function () {
        this.getMovies();
    },
    methods: {
        getMovies: function () {
            this.$http.get(remoteUrl).then(response => {
                this.items = response.body;
            }, response => {
                alert(response);
            });
        }
    }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to ensure my arrow text remains properly positioned when using fullpage.js?

Seeking guidance from the web development community. I am currently working on a client's website that utilizes fullpage.js and encountering a persistent issue. On slide1 of the website, I am struggling to display the correct text next to the arrows. ...

CSS: The tooltip's shadow in Safari persists

In my design, I have implemented a set of 3 buttons, each with its own specific tooltip containing messages. While the functionality works flawlessly on Windows when the tooltip is displayed and hidden, I have encountered an issue on iOS, particularly in S ...

Serialize a form that was dynamically generated and then make an AJAX request to post

I am dealing with some dynamically generated content that includes a small form (dropdown with button) for assigning tasks to someone. With the help of jQuery, my goal is to update my database with the newly assigned person. This is the approach I am taki ...

Leveraging JavaScript Functions in HTML

I am having an issue with a JavaScript file containing two similar functions that are executed through an HTML form. While the first function runs smoothly, the second function does not display correctly. It seems like I might be calling or executing the ...

View the edited image preview instantly upon selecting the image

I have selected an image and previewed it before submitting the form. However, now I wish to be able to edit the file immediately after selecting it, preview the changes, and then submit the file. <input type ="file" accept="image/*" id="image" name="i ...

Error: The specified element "div" configuration is not found in the view

Trying to include a <div> element in a react-native project, but encountering an error message in the simulator: Invariant Violation: View config not found for name div Is there a way to render <div> in react-native without issues? Sample code ...

As the user types, automatically format the input field for a seamless and intuitive experience

My current application is running on Angular 1.3.10 I have implemented a jQuery function to automatically add a backslash to the expiration input field once the user types the third number. Although it was a quick fix, I now aim to move this functionality ...

The $scope.$watch function is not triggering events within a controller of a ui.bootstrap.modal

Currently, I am utilizing UI bootstrap for Angular and have successfully integrated the ui.bootstrap.modal component into my project. Everything seems to be working smoothly except for one issue I am encountering. Despite setting up a $scope.$watch to trac ...

Launching the forEach function within an ng-repeat loop was it can be done by

I need to implement a function within the ng-repeat that will convert the value of Qprogress object in my JSON into a percentage. I already have the function written, but I am struggling with how to trigger it. I attempted to use a forEach loop inside the ...

What is the correct way to establish and terminate a MongoDB connection in a Node.js application?

Hey everyone, I have a piece of code at this link (https://github.com/MrRav3n/Angular-Marketplace/blob/master/server.js) and I'm curious if I am properly starting and ending my database connection. Should I connect and end the db connection in every a ...

What is the best way to send a parameter to a mousewheel event?

for (var i = 0; i < 10; i++) { $('#content-scroll' + i).mousewheel(function(event, delta) { if (delta > 0) sliderUp(i - 1); else if (delta < 0) sliderDown(i - 1); return false; // prevent default }); } I am facing an iss ...

What is causing this JavaScript function to output '2'?

Recently, I came across an unusual JavaScript function: (function f(){ function f(){ return 1; } return f(); function f(){ return 2; } })(); To my surprise, it returns 2 instead of crashing the browsers as expected due to recursion. Curious ...

Tips for successfully implementing Angular.js validation within the confines of a <form> element

Having trouble getting my code to work when I place my app inside the <form name="myForm"> tag. Any suggestions on how to make it function properly? (It works when I place myForm inside ng-app, but my app needs to be within the <form> tag) < ...

I encountered a console issue that I am struggling with. It is showing the error message "TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'"

When running this code, I encountered an error in the console (TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'). Can someone help me identify where I made a mistake and provide guidance ...

Update the content retrieved through ajax periodically

Looking to set up a periodic update for this ajax fetch function, say every 10 seconds. I've tried a few approaches but none seem to work as expected. That's why I'm reaching out here for help. So, any idea how I can refresh the content ev ...

Implement the ability to sort datetime columns in Vue Tables 2

I am currently working on a custom component that utilizes the v-client-table method from Vue Tables 2. The table consists of two columns, with one of them being a datetime column formatted as MM/DD/YYYY h:mm A. Although I have set it to be sortable, it do ...

Organizing DIVs upon website initialization

My website features a layout with three columns: <div id="column1"></div> <div id="column2"></div> <div id="column3"></div> I currently have 3 divs on the webpage: <div id="1">aaa</div> <div id="2">b ...

How to position a div in the center of the screen with an overlay

While this code functions well in Firefox, it unfortunately does not work in IE. I am seeking a solution to properly center the div on the screen - Currently, the div is leaning too much towards the bottom In addition, I require the div with an id of "l ...

Can you explain the difference between CDN and ESM builds in vue.js?

According to the Vue.js documentation, there are differences in syntax depending on whether you are using the CDN or ESM build of Vue.js. What is the significance of having two different builds and how does it result in a difference in usage syntax? Infor ...

Fade effect not working with Bootstrap Modal

I am encountering an issue with the Twitter Bootstrap modal while trying to display post details on WordPress. The problem is that when the modal opens, the entire screen turns grey and nothing in the modal is clickable. I can only exit this mode by pressi ...