【发布时间】:2018-12-09 03:07:57
【问题描述】:
// 这是来自本地的它是 json 对象 { “身份证”:654321, “姓名”:“莫哈德”, “年龄”:22 }, { “身份证”: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对象{
“国家”:“美国”,
“州”:“纽约”,
“城市”:“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)
}
})
为什么当我在本地使用它时不起作用
【问题讨论】:
-
澄清您的要求。如果您问为什么调用
localhost端点不起作用,请调试您的本地端点。在进行 XHR 调用等之前对其进行单元测试。 -
解释什么不起作用。