var ajax = function () {
// 做一些初始化,定义一些私有函数等
return function () {
// ajax主体代码
}
}()
ajax({
url: myUrl,
type: 'get',
dataType: 'json',
timeout: 1000,
success: function (data, status) {
console.log(data)
},
fail: function (err, status) {
console.log(err)
}
})
//默认请求参数
var _options = {
url: null, // 请求连接
type: 'GET', // 请求类型
data: null, // post时请求体
dataType: 'text', // 返回请求的类型,有text/json两种
jsonp: 'callback', // jsonp请求的标志,一般不改动
jsonpCallback: 'jsonpCallback', //jsonp请求的函数名
async: true, // 是否异步
cache: true, // 是否缓存
timeout:null, // 设置请求超时
contentType: 'application/x-www-form-urlencoded',
success: null, // 请求成功回调函数
fail: null // 请求失败回调
}
var ajax = function () {
//默认请求参数
var _options = {
url: null,
type: 'GET',
data: null,
dataType: 'text',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
async: true,
cache: true,
timeout:null,
contentType: 'application/x-www-form-urlencoded',
success: null,
fail: null
}
// ...
return function (options) {
// ...
}
}()
var ajax = function () {
//默认请求参数
var _options = {
url: null,
type: 'GET',
data: null,
dataType: 'text',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
async: true,
cache: true,
timeout:null,
contentType: 'application/x-www-form-urlencoded',
success: null,
fail: null
}
// 内部使用的继承方法
var _extend = function(target,options) {
if( typeof target !== 'object' || typeof options !== 'object' ) {
return;
}
var copy ,clone, name;
for( name in options ) {
if(options.hasOwnProperty(name) && !target.hasOwnProperty(name)) {
target[name] = options[name];
}
}
return target;
};
// ...
return function (options) {
// 没有传参或者没有url,抛出错误
if( !options || !options.url ) {
throw('参数错误!');
}
// 继承操作
options.type = options.type.toUpperCase();
_extend(options,_options);
// ...
}
}()
var ajax = function () {
//默认请求参数
var _options = {
url: null,
type: 'GET',
data: null,
dataType: 'text',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
async: true,
cache: true,
timeout:null,
contentType: 'application/x-www-form-urlencoded',
success: null,
fail: null
}
// 内部使用的继承方法
var _extend = function(target,options) {
if( typeof target !== 'object' || typeof options !== 'object' ) {
return;
}
var copy ,clone, name;
for( name in options ) {
if(options.hasOwnProperty(name) && !target.hasOwnProperty(name)) {
target[name] = options[name];
}
}
return target;
};
// jsonp处理函数
function _sendJsonpRequest(url,callbackName,succCallback) {
var script = document.createElement('script');
script.type="text/javascript";
script.src=url;
document.body.appendChild(script);
// 如果用户自己定义了回调函数,就用自己定义的,否则,调用success函数
window[callbackName] = window[callbackName] || succCallback;
}
// ...
return function (options) {
// 没有传参或者没有url,抛出错误
if( !options || !options.url ) {
throw('参数错误!');
}
// 继承操作
options.type = options.type.toUpperCase();
_extend(options,_options);
/*jsonp部分,直接返回*/
if( options.dataType === 'jsonp' ) {
var jsonpUrl = options.url.indexOf('?') > -1 ? options.url: options.url +
'?' + options.jsonp+ '=' + options.jsonpCallback;
return _sendJsonpRequest(jsonpUrl,options.jsonpCallback,options.success);
}
// ...
}
}()
var ajax = function () {
//默认请求参数
var _options = {
url: null,
type: 'GET',
data: null,
dataType: 'text',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
async: true,
cache: true,
timeout:null,
contentType: 'application/x-www-form-urlencoded',
success: null,
fail: null
}
// 内部使用的继承方法
var _extend = function(target,options) {
if( typeof target !== 'object' || typeof options !== 'object' ) {
return;
}
var copy ,clone, name;
for( name in options ) {
if(options.hasOwnProperty(name) && !target.hasOwnProperty(name)) {
target[name] = options[name];
}
}
return target;
};
// jsonp处理函数
function _sendJsonpRequest(url,callbackName,succCallback) {
var script = document.createElement('script');
script.type="text/javascript";
script.src=url;
document.body.appendChild(script);
// 如果用户自己定义了回调函数,就用自己定义的,否则,调用success函数
window[callbackName] = window[callbackName] || succCallback;
}
// json转化为字符串
var _param = function(data) {
var str = '';
if( !data || _empty(data)) {
return str;
}
for(var key in data) {
str += key + '='+ data[key]+'&'
}
str = str.slice(0,-1);
return str;
}
//判断对象是否为空
var _empty = function(obj) {
for(var key in obj) {
return false;
}
return true;
}
// ...
return function (options) {
// 没有传参或者没有url,抛出错误
if( !options || !options.url ) {
throw('参数错误!');
}
// 继承操作
options.type = options.type.toUpperCase();
_extend(options,_options);
/*jsonp部分,直接返回*/
if( options.dataType === 'jsonp' ) {
var jsonpUrl = options.url.indexOf('?') > -1 ? options.url: options.url +
'?' + options.jsonp+ '=' + options.jsonpCallback;
return _sendJsonpRequest(jsonpUrl,options.jsonpCallback,options.success);
}
//XMLHttpRequest传参无影响
var xhr = new (window.XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP');
// get搜索字符串
var search = '';
// 将data序列化
var param= _param(options.data)
if( options.type === 'GET' ) {
search = (options.url.indexOf('?') > -1 ? '&' : '?') + param;
if(!options.cache) {
search += '&radom='+Math.random();
}
param = null;
}
// ...
}
}()
;
var ajax = function () {
//默认请求参数
var _options = {
url: null,
type: 'GET',
data: null,
dataType: 'text',
jsonp: 'callback',
jsonpCallback: 'jsonpCallback',
async: true,
cache: true,
timeout:null,
contentType: 'application/x-www-form-urlencoded',
success: null,
fail: null
}
// json转化为字符串
var _param = function(data) {
var str = '';
if( !data || _empty(data)) {
return str;
}
for(var key in data) {
str += key + '='+ data[key]+'&'
}
str = str.slice(0,-1);
return str;
}
//判断对象是否为空
var _empty = function(obj) {
for(var key in obj) {
return false;
}
return true;
}
var _extend = function(target,options) {
if( typeof target !== 'object' || typeof options !== 'object' ) {
return;
}
var copy ,clone, name;
for( name in options ) {
if(options.hasOwnProperty(name) && !target.hasOwnProperty(name)) {
target[name] = options[name];
}
}
return target;
};
// 自定义text转化json格式
var parseJSON = function(text) {
if(typeof text !== 'string') {
return;
}
if( JSON && JSON.parse ) {
return JSON.parse(text);
}
return (new Function('return '+text))();
}
// jsonp处理函数
function _sendJsonpRequest(url,callbackName,succCallback) {
var script = document.createElement('script');
script.type="text/javascript";
script.src=url;
document.body.appendChild(script);
// 如果用户自己定义了回调函数,就用自己定义的,否则,调用success函数
window[callbackName] = window[callbackName] || succCallback;
}
return function (options) {
// 没有传参或者没有url,抛出错误
if( !options || !options.url ) {
throw('参数错误!');
}
// 继承操作
options.type = options.type.toUpperCase();
_extend(options,_options);
/*jsonp部分,直接返回*/
if( options.dataType === 'jsonp' ) {
var jsonpUrl = options.url.indexOf('?') > -1 ? options.url: options.url +
'?' + options.jsonp+ '=' + options.jsonpCallback;
_sendJsonpRequest(jsonpUrl,options.jsonpCallback,options.success);
return;
}
//XMLHttpRequest传参无影响
var xhr = new (window.XMLHttpRequest || ActiveXObject)('Microsoft.XMLHTTP');
// get搜索字符串
var search = '';
// 将data序列化
var param= _param(options.data)
if( options.type === 'GET' ) {
search = (options.url.indexOf('?') > -1 ? '&' : '?') + param;
if(!options.cache) {
search += '&radom='+Math.random();
}
param = null;
}
xhr.open( options.type, options.url + search, options.async );
xhr.onreadystatechange = function() {
if( xhr.readyState == 4 ) {
if( xhr.status >= 200 && xhr.status < 300 || xhr.status == 304 ) {
var text = xhr.responseText;
// json格式转换
if(options.dataType == 'json') {
text = parseJSON(text)
}
if( typeof options.success === 'function') {
options.success(text,xhr.status)
}
}else {
if(typeof options.fail === 'function') {
options.fail('获取失败', 500)
}
}
}
}
xhr.setRequestHeader('content-type',options.contentType);
// get请求时param时null
xhr.send(param);
// 如果设置了超时,就定义
if(typeof options.timeout === 'number') {
// ie9+
if( xhr.timeout ) {
xhr.timeout = options.timeout;
}else {
setTimeout(function() {
xhr.abort();
},options.timeout)
}
}
}
}()
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有