首页 > 其他 > 详细

vue-resource

时间:2020-07-25 20:41:32      阅读:59      评论:0      收藏:0      [点我收藏+]
  1. 下载npm i vue-resource
  2. main.js中引入使用:
import VueResource from ‘vue-resource‘
Vue.use(VueResource);

.vue文件中使用:

get请求:

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(‘失败了‘)
    })
  }
}

post请求:

		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 告诉后台要传输的数据类型,即:请求命令行

vue-resource

原文:https://www.cnblogs.com/yxq-funny-soul/p/13376340.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!