我如何在 vue js 中从 this.$http.get 返回数据

use*_*670 4 ajax vue.js laravel-5

嗨,我在 Laravel 5 中使用 vue js 在我的 ajax 调用中返回数据时遇到问题。我有一个状态数组并在循环内调用 ajax 的函数。现在的问题是 ajax 似乎无法返回值。这是我的代码:

ready: function() {

    var dData = {};

    for (var i=0; i<this.pState.selectedState.length; i++){
        dData = this.displayCounty(this.pState.selectedState[i])
    }
    console.log(dData);

},
methods:{

    displayCounty: function(val){
        var nData = {};

        // ajax get County list
        this.$http.get('/api/counties/' + val )
            .success(function(counties){
               return counties;
            })
            .error(function(){

            }) //ajaxcall

    }// displaCounty
}
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?

小智 5

如果您只想将返回的结果分配给变量,请尝试以下操作:

ready: function() {
    this.getCounties;
},

data: {
    counties: []
},

methods:{

    getCounties: function(val){

        // ajax get County list
        this.$http.get('/api/counties/' + val )
            .success(function(counties){
               this.counties = counties;
            })
            .error(function(){

            }); //ajaxcall

    } // displayCounty
}
Run Code Online (Sandbox Code Playgroud)

  • 怎么样,如果我不想将该结果分配给国家数据? (2认同)