Javascript/vue.js收到json

Jam*_*mie 7 javascript json vue.js

我想在我的vue.js应用程序中接收json,如下所示:

  new Vue({
            el: 'body',
            data:{
                role: '',
                company: '',
                list:[],
                created: function() {
                  this.getJson();
                },
                methods: {
                    getJson: function(){
                        $.getJSON('http://domain.dev/data',function(task){
                          this.list = task;
                        }.bind(this));
                    }
                }
            }
        });
Run Code Online (Sandbox Code Playgroud)

但结果是空的?当我在邮递员中测试这个时,url返回json.我在这做错了什么?

编辑:

JSON(testdata):

{"EmployeeId":1,"RoleId":5,"DepartmentId":6,"InternId":1,"FirstName":"Zoe","LastName":"Altenwerth","Bio":"Quidem perferendis.","email":"Kole.Bechtelar@hotmail.com","LinkedIn":"Sterling.Schowalter@example.net","Gender":0,"password":"$2y$10$bbUlDh2060RBRVHSPHoQSu05ykfkw2hGQa8ZO8nmZLFFa3Emy18gK","PlainPassword":"gr^S=Z","remember_token":"D528C0Ba1Xzq3yRV7FdNvDd8SYbrM0gAJdFUcOBq4sNEJdHEOb2xIQ0geVhZ","Address":"0593 Dallin Parkway Apt. 499\nBotsfordborough, MT 12501","Zip":"21503-","City":"East Janiston","ProfilePicture":null,"BirthDate":"2002-10-13 00:00:00","StartDate":"1995-11-09 21:42:22","EndDate":"2011-01-27","Suspended":0,"created_at":"2016-02-29 12:21:42","updated_at":"2016-03-02 11:53:58","deleted_at":null,"role":{"RoleId":5,"RoleName":"Superadministrator","Description":"Mag administrators toevoegen en bewerken","deleted_at":null,"created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00"},"department":{"DepartmentId":6,"CompanyId":12,"DepartmentName":"com","Description":"Accusantium quae.","deleted_at":null,"created_at":"2016-02-29 12:21:41","updated_at":"2016-02-29 12:21:41","company":{"CompanyId":12,"CompanyName":"Dare, Bailey and Bednar","Logo":null,"Address":"85762 Tabitha Lights\nWest Jettie, AK 20878-2569","Zip":"29601","City":"Traceside","KvKNumber":"84c70661-9","EcaboNumber":"fdee61e3-a22d-3332-a","deleted_at":null,"created_at":"2016-02-29 12:21:41","updated_at":"2016-02-29 12:21:41"}}}
Run Code Online (Sandbox Code Playgroud)

vba*_*osh 21

以下是如何将外部JSON数据加载到组件中的一个小示例:

a.json:

{"hello": "welcome"}
Run Code Online (Sandbox Code Playgroud)

index.html的:

<div id="app">
    <pre>{{ json.hello }}</pre>
</div>

<script type="text/javascript">
var app = new Vue({
    el: '#app',
    data: {
        json: null
    }
});
$.getJSON('http://localhost/a.json', function (json) {
    app.json = json;
});
</script>
Run Code Online (Sandbox Code Playgroud)

---编辑---

或者有created活动:

<script type="text/javascript">
new Vue({
    el: '#app',
    data: {
        json: null
    },
    created: function () {
        var _this = this;
        $.getJSON('http://localhost/a.json', function (json) {
            _this.json = json;
        });
    }
});
</script>
Run Code Online (Sandbox Code Playgroud)

  • @SuatAtanPhD有三种方法可以访问`this`.1)使用像`_this`这样的变量; 2)使用箭头函数`()=> {...}`; 3)使用bind`function(){}.bind(this)`.我选择第一个选项. (4认同)

ant*_*ine 11

建立@ vbarbarosh的答案,但使用浏览器的fetch api:

a.json:

{"hello": "welcome"}
Run Code Online (Sandbox Code Playgroud)

index.html的:

<div id="app">
    <pre>{{ json.hello }}</pre>
</div>

<script type="text/javascript">
new Vue({
    el: '#app',
    data: {
        json: null
    },
    created: function () {
      fetch("/a.json")
        .then(r => r.json())
        .then(json => {
          this.json=json;
        });
    }
});
</script>
Run Code Online (Sandbox Code Playgroud)


Lar*_*ner 5

你也必须绑定this到外部函数.

getJson: function () { ...}.bind(this)
Run Code Online (Sandbox Code Playgroud)