weixin_33737134 2017-06-09 09:58 采纳率: 0%
浏览 35

AJAX和Vue.js 2无法正常工作

I'm trying to use AJAX 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("ERRORE: " + err);
            });
        }
    }

});

this is the html:

            <table>
                <tbody>
                    <tr v-for="item in items">
                        <td>{{item.nome}}</td>
                        <td>{{item.data}}</td>
                        <td>{{item.size}}</td>
                        <td>{{item.ext}}</td>
                    </tr>
                </tbody>
            </table>
        </div>

i see the output in console. But the table remains empty. I do not see any errors in the AJAX call. I tried both with created and with mounted. why?

  • 写回答

3条回答 默认 最新

  • weixin_33690367 2017-06-09 10:13
    关注

    You just need to add let self = {} below your remoteUrl declaration, unless you already declared self somewhere else.

    This should fix the problem (if nome, data, size and ext were properties of your res that is).

    评论

报告相同问题?