2017-05-25 75 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}} 

在你的模板。