Vue/jQuery 的 GET/POST 请求

1. Vue 的 GET 请求

var vm = new Vue({
	el: '#app',
	data: {
		resp: {},
		api_url: '/index',
},
	methods: { 
		get_data(){
			this.$http.get(this.api_url)
			.then((response) => {
			    // 用 set 将响应结果赋给变量 resp
				this.$set(this.resp,'data',response.body)
			}).catch(function(response){console.log(response)})
		}
	}
})

2. Vue 的 POST 请求

var vm = new Vue({
	el: '#app',
	data: {
		resp: {},
		post_data: {'name':'abc'},
		api_url: '/index',
},
	methods: { 
		get_data(){
			this.$http.post(this.api_url,this.post_data,{emulateJSON:true})
				.then((response) => {this.$set(this.resp,'data',response.body)})
				.catch(function(response){console.log(response)})
		}
	}
})

3. jQuery 的 GET 请求

$(function(){
	$("#btn").click(function(){
		$.ajax({
			url:"/index",
			dataType:"json",
			data:{name:'abc'},
			type:"get",
			success:function(resp){
				var result
				result = JSON.parse(resp);
				}
			})
		})
	})

4. jQuery 的 POST 请求

$("btn").click(function(){
	$.post(url,{user:'abc',pwd:'******'},function(resp){
       if(resp.success){
          $.messager.alert("系统提示","添加成功","info");
       }else{
          $.messager.alert("系统提示","添加失败","error");
       }
    },"json");
}

猜你喜欢

转载自blog.csdn.net/weixin_42902669/article/details/88623450