1. Vue 的 GET 请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
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)}) } } }) axios .post('https://www.runoob.com/try/ajax/json_demo.json',{id:id})
.then(function (response) { console.log(response); }) .catch(function (error) {
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");
}