详解vue前后台数据交互vue-resource文档

2020-06-13 10:41:31易采站长站整理

params: {
cat: 1
},
method: 'JSONP'
})
.then(function ( response ){

}, function () {
//error

});

实例底层方法


http.$http({
url: 'http://example.com/books',
params: {
cat: 1
},
method: 'JSONP'
})
.then(function () {
// this 指向当前组件实例
}, function () {

});

实例便捷方法


this.$http.jsonp(
'http://www.example.com/books',
{
cat: 1
}
)
.then(function () {

}, function () {

});

修改数据类型

如何修改发送给服务端的数据类型

在默认情况下,对于PUT,PSOT,PATCH,DELETE等请求,请求头中的Content-Type为appliaction/json即JSON类型。有时候需要将数据提交为指定类型如application/x-www-form-urlencoded,multipart/form-data,txt/plain等。

全局headers配置


Vue.http.heaers.post['Content-Type'] = 'application/x-www-form-urlencoded'

实例配置


this.$http.post(
'http://example.com/books',
// 成功回调
function ( data, status, request ) {
if ( status == 200 ) {
consl.dir(data);
}
},
// 配置请求头
headres: {
'Content-Type': 'multipart/form-data'
}
);
// 实例配置的优先级高于全局配置

跨域请求出错

跨域请求需要服务端开启CORS支持