vue交互方式
get
get交互如下所示:两个方法,一个成功,一个失败。
methods:{ get: function () { this.$http.get('1-1.html').then(function (res) { res是一个对象,里面包含状态码,包含数据 console.log(res.data); }, function () { console.log(2) }) } } 与服务器做交互: methods:{ get: function () { this.$http.get('a.php',{ a:1,b:2 //后面是发送给服务器的参数 }).then(function (res) { console.log(res.data); }, function () { console.log(2) }) } }
post
methods:{ get: function () { this.$http.post('a.php',{ a:1,b:2 },{ emulateJSON:true //post提交需要写一个这样的声明 }).then(function (res) { console.log(res.data); }, function () { console.log(2) }) } }
jsonp
接口
https://sug.so.360.cn/suggest?callback=suggest_so&word=a <input type="button" value="按钮" @click="get()"> methods:{ get: function () { this.$http.jsonp('https://sug.so.360.cn/suggest',{ word:'a' },{ }).then(function (res) { console.log(res.data.s); }, function (res) { console.log(res.status); }); } }