//这是来自本地的json对象 { “ Id”:654321, “ Name”:“ Mohd”, “年龄”:22 }, { “ Id”:102030, “名称”:“法赫德”, “年龄”:18 } //这是我的代码不起作用
var app = new Vue({
el: '#app',
data: {
socialId: '',
person: ''
},
watch: {
socialId: function() {
this.person = ''
if (this.socialId.length == 6) {
this.lookupsocialId()
}
}
},
methods: {
lookupsocialId: _.debounce(function() {
var app = this
app.person = "Searching..."
axios.get('http://localhost:49999/api/people/' + app.socialId)
.then(function (response) {
app.person = response.data.Name + ', ' + response.data.Age
})
.catch(function (error) {
app.person = "Invalid Zipcode"
})
}, 500)
}
})
但是我曾经使用过,它运行得很好
json对象{
“ country”:“ US”,
“ state”:“ NY”,
“ city”:“ SCHENECTADY”
}
var app = new Vue({
el: '#app',
data: {
socialId: '',
person: ''
},
watch: {
socialId: function() {
this.person = ''
if (this.socialId.length == 5) {
this.lookupsocialId()
}
}
},
methods: {
lookupsocialId: _.debounce(function() {
var app = this
app.person = "Searching..."
axios.get('http://ziptasticapi.com/' + app.socialId)
.then(function (response) {
app.person = response.data.city
})
.catch(function (error) {
app.person = "Invalid Zipcode"
})
}, 500)
}
})
为什么在本地使用它不起作用
答案 0 :(得分:0)
您需要将结果保存到json文件中,使用axio response.json()后,可以尝试以下代码
<script>
export default{
data(){
return{
title:"Form Register",
formdata:{},
message:"",
success:0,
}
},
methods:{
register(){
this.axios.post("http://localhost:8888/form-register",this.formdata).then((response) => {
console.log(response);
if(response.data.success>0){
this.message="You register success";
this.success=response.data.success;
}
else{
this.message="Register to failed";
this.success=response.data.success;
}
});
},
}
}
</script>
//construct json
[
{
name:"abc"
age:34
},
{
name:"abc"
age:34
}
]
if use php, you can echo json_encode() in php