jsonp:
1、axios不支持jsonp,但是有插件,试了不好用
2、vue-resource支持jsonp,试了()
3、自己写的用jquery ajax封装的jsonp
vue版
let prod_url = '//bbbb'; // prod
let dev_mock_url = '//eeee'; // dev
let url = window.location.hostname !== 'localhost' ? prod_url : dev_mock_url;
export const fetchData = function (options, ifonline) {
let _url = ifonline ? prod_url : url;
return new Promise((resolve, reject) => {
let config = $.extend({
url: '',
type: 'GET',
data: {}
}, options);
config.url = _url + config.url;
if (config.cors) {
config.xhrFields = {withCredentials: true};
} else if (config.jsonp) {
config.dataType = 'jsonp';
config.data = {
data: JSON.stringify(config.data)
}
typeof config.jsonp === 'boolean' && delete config.jsonp;
}
$.ajax(config)
.done(function (data) {
if (data.responseCode === '00000') {
resolve(data)
} else {
reject(data)
}
})
.fail(function (err) {
reject({responseData: '网络错误, 请稍后重试'});
});
})
};
export const fetchData_old = function (options) {
let _url = url;
let config = $.extend({
url: '',
type: 'GET',
data: {}
}, options);
config.url = _url + config.url;
if (config.cors) {
config.xhrFields = {
withCredentials: true
}
} else if (config.jsonp) {
config.dataType = 'jsonp';
config.data = {
data: JSON.stringify(config.data)
}
if (typeof config.jsonp === 'boolean') {
delete config.jsonp;
}
}
return $.ajax(config)
};
jquery版
;
(function(global) {
var prod_url = '//ft.jdpay.com/hapi/'; // prod
var dev_mock_url = '//mock.jdfmgt.com/mock/58f038b9d3bb8c02798eda1d/'; // dev
global.__url = window.location.hostname!=='localhost'? prod_url : dev_mock_url;
global.fetchData = function(options, ifonline) {
var self = this,
dtd = $.Deferred(),
_url = ifonline ? prod_url : __url;
var config = $.extend({
url: '',
type: 'GET',
data: {}
}, options);
config.url = _url + config.url;
if (config.cors) {
config.xhrFields = { withCredentials: true };
}else if (config.jsonp) {
config.dataType = 'jsonp';
config.data = {
data: JSON.stringify(config.data)
}
typeof config.jsonp === 'boolean' && delete config.jsonp;
}
$.ajax(config)
.done(function(data){
if(data.responseCode === '00000') {
dtd.resolve(data);
} else if(data.responseCode === 'JRO3030005'){
// 未开通企业金库
if(window.location.pathname !== '/accountop/active.html'){
window.location.href = './active.html';
}
} else {
dtd.reject(data);
}
})
.fail(function(err) {
dtd.reject({ responseData: '网络错误, 请稍后重试' });
});
return dtd.promise();
}
global.fetchData_old = function(options) {
var self = this;
var _url = __url;
var config = $.extend({
url: '',
type: 'GET',
data: {}
}, options);
config.url = _url + config.url;
if (config.cors) {
config.xhrFields = {
withCredentials: true
}
} else if (config.jsonp) {
config.dataType = 'jsonp';
config.data = {
data: JSON.stringify(config.data)
}
if (typeof config.jsonp === 'boolean') {
delete config.jsonp;
}
}
return $.ajax(config)
}
})(typeof window !== 'undefined' ? window : this);