npm i vue-resource
import VueResource from ‘vue-resource‘
Vue.use(VueResource);
.vue文件中使用:
export default {
name: ‘app‘,
data () {
return {
msg: ‘Welcome to Your Vue.js App‘
}
},
mounted() {
// console.log(this.$http);
this.$http.get(‘http://localhost:8506/login?a=1‘).then((res)=>{
console.log(res.data);
}).catch(()=>{
console.log(‘失败了‘)
})
}
}
methods:{
login:function(){
//console.log(this.$http)
this.$http.post(‘http://localhost:2133/getPost‘,{
user:this.user,
pass:this.password
},{
emulateJSON:true
}).then(function(data){
//console.log(data)
if(data.data.ok == 1){
alert(data.data.msg)
}
else{
alert(‘失败了‘)
}
}).catch(function(){
alert(‘比较大的问题‘)
})
}
}
注意:post请求中添加了
emulateJSON:true
相当于ajax中的application/x-www-form-urlencodedContent-Type
告诉后台要传输的数据类型,即:请求命令行
原文:https://www.cnblogs.com/yxq-funny-soul/p/13376340.html