2017-05-25 77 views
0

我是Vue.js中的新成员。我想从ajax数据更新数据。Vue.js更新数据属性变量中的ajax数据

<template> 
    <span class="label_cont">This is to certify that</span> {{ name }} 
</template> 

我想要{{ name }}变量从ajax数据。
这里是我的js代码

export default{ 
    created(){ 
    const url = 'app/result.php'; 
    this.$http.get(url).then(response => { 
     this.resp = response.body; 
    }); 
    }, 
    data(){ 
    return { 
    resp:[], 
    name: resp.name, 
    } 
    } 
} 

在我的AJAX name属性是this.resp.name

更新:

这里是我的Ajax响应数据格式

{ 
    "name": "X", 
    "class": "Y", 
    "roll": "Z" 
} 
+0

你有什么数据response.body?向我们显示数据格式 –

+0

我已经更新了数据格式 – alien

回答

2

添加在下面的回调中多加一行。

this.$http.get(url).then(response => { 
    this.resp = response.body; 
    this.name = response.name 
}); 

在您的数据刚刚初始化为空。

data(){ 
    return { 
    resp:[], 
    name: null, 
    } 
} 

或者你可以只使用

{{resp.name}} 

在你的模板。