/*
Name : kessTool
Type : javascript
Fun : Tool
Date : 2016年8月23日 16:13:15
by : kess
note : 重写
*/
(function (window, undefined) {
//获取的是元素的 element 类型
var Kess = function (selector, context) {
return new Kess.fn.init(selector, context);
};
//要实现一个选择器 作用就是 选择 dom节点
//对对象进行处理
Kess.fn = Kess.prototype = {
//初始化 元素
init: function (selector, context) {
//证明,直接传过来的是 document 对象
if (selector.nodeType === 1) {
//那么直接把这个对象传过去
this[0] = selector;
this.length = 1;
return this;
}
else if (typeof selector == "string") {
var obj = document.getElementById(selector.replace("#", ""));
if (obj != null && obj.nodeType === 1) {
this[0] = obj;
this.length = 1;
return this;
}
}
//否则就查找出这个对象
var obj = context || document;
var noteList = obj.querySelectorAll(selector);//获取全部对象列表
this.length = noteList.length;
for (var i = 0; i < this.length; i += 1) {
this[i] = noteList[i];
}
return this;
}
}
//实现扩展方法
Kess.extend = Kess.fn.extend = function (source) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
this[property] = source[property];
}
}
return this;
};
//原型传递。
Kess.fn.init.prototype = Kess.prototype;
//设置为全局变量
window.Kess = Kess;
})(window);//把全局变量给传递进去
//下边写一些工具的扩展方法
Kess.extend({
//判断是否是window对象
isWindow: function (obj) {
return obj != null && obj.window == window;
},
//判断是否为对象
isObject: function (obj) {
return Object.prototype.toString.call(obj) == "[object Object]";
},
//判断是否是 数组
isArray: function (obj) {
return Object.prototype.toString.call(obj) == "[object Array]";
},
//判断是否是 函数
isFunction: function (obj) {
return Object.prototype.toString.call(obj) == "[object Function]";
},
//判断是否是 字符串
isString: function (obj) {
return Object.prototype.toString.call(obj) == "[object String]";
},
//判断是否是布尔变量
isBool: function (obj) {
return Object.prototype.toString.call(obj) == "[object Boolean]";
},
//判断字符串是否为空
isEmpty: function (obj) {
return (obj == "" && this.isString(obj));
},
//判断是否为null或""
isNullOrEmpty: function (obj) {
if (typeof (obj) == "undefined") {
return true;
} else if (obj == null) {
return true;
} else if (obj == '') {
return true;
} else {
return false;
}
},
//判断是否为null
isNull: function (obj) {
return (obj == null && this.isObject(obj));
},
//把对象转变为 URL 参数
toUrlParames: function (obj) {
var str = "";
if (!this.isNull(obj)) {
for (var item in obj) {
str += item + '=' + obj[item] + '&';
}
str = str.slice(0, str.length - 1);//去掉最后一个&号
} else if (this.isString(obj)) {
str = obj;
}
return str;
},
//把一个对象的内容放入到另外一个对象里面
toObject: function (old, xin, IsMore) {
if (!this.isNull(old) && !this.isNull(xin)) {
for (var items in xin) {
if (IsMore) {
old[items] = xin[items];
} else {
if (old.hasOwnProperty(items)) {
old[items] = xin[items];
}
}
}
}
return old;
}
});
//下边是自己写的一些常用远程调用的方法
Kess.extend({
//设置ajax默认值
ajaxSettings: {
url: location.href,
global: true,
type: "POST",
contentType: "application/x-www-form-urlencoded",
processData: true,
async: true,
cache: false,
accepts: {
xml: "application/xml, text/xml",
html: "text/html",
script: "text/javascript, application/javascript",
json: "application/json, text/javascript",
text: "text/plain",
_default: "*/*"
}
},
//ajaxCore ajax 核心方法
ajaxCore: function (config) {
//数据初始化默认值
var success = config.success || function () { },
faill = config.faill || function () { },
async = Kess.isBool(config.async) ? config.async : this.ajaxSettings.async,
cache = Kess.isBool(config.cache) ? config.cache : this.ajaxSettings.cache,
processData = Kess.isBool(config.processData) ? config.processData : this.ajaxSettings.processData,
type = config.type ? config.type.toUpperCase() : this.ajaxSettings.type,
xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP'),
timeT = "_t=" + Math.random(),
url = config.url || window.location.href.split('#')[0],
canshu = processData ? Kess.toUrlParames(config.data) : config.data;
//先获取参数
if (type == "GET") {
url += config.url.indexOf('?') == -1 ? '?' + canshu : '&' + canshu;
}
//处理参数
config.data = "";
//获取数据
xmlhttp.open(type, config.url, async);
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
console.log(xmlhttp.getResponseHeader("content-type"));
if (!Kess.isObject(xmlhttp.responseText)) {
success(JSON.parse(xmlhttp.responseText));
}
else {
success(xmlhttp.responseText);
}
}
else {
faill(xmlhttp);
}
}
}
xmlhttp.send(config.data);
},
//ajax方法
ajax: function (config) {
var type = config.type || 'POST', processData = this.isBool(config.processData) ? config.processData : true, dataType = config.dataType;
config.type = type = type.toUpperCase();//全部转换为大写
config.url = config.url || window.location.href.split('#')[0];//对url进行赋值
if (processData) {
if (Kess.isObject(config.data)) {
if (dataType == "json") {
config.data = JSON.stringify(config.data);
}
else {
config.data = this.toUrlParames(config.data);
}
}
}
if (type == 'GET') {
config.url += config.url.indexOf('?') == -1 ? '?' + config.data : '&' + config.data;
}
if (dataType == "json") {
config.ContentType = "application/json; charset=utf-8";
}
this.ajaxCore(config);
},
//From 异步上传
submit: function (config) {
var data = config.data, temp;
//如果表单名称存在
if (!Kess.isEmpty(config.formName)) {
temp = new FormData(document.forms.namedItem(config.formName));
}
//就伪装成表单进行发送
if (!this.isNull(data)) {
if (this.isNull(temp)) {
temp = new FormData();
}
for (var item in data) {
temp.append(item.toString(), data[item]);
}
}
if (!this.isNull(temp)) {
config.data = temp;
}
config.setHeader = false;
config.type = "POST";
//开始把它发送出去
this.ajaxCore(config);
},
//getJson jsonp 跨域获取
getJson: function (config) {
var callname = config.callname || "callback", callback = config.callback || 'Kess' + parseInt(Math.random() * 1000), success = config.success || function () { };
config.data = config.data ? config.data : config.data = new Object();
config.data[callname] = callback;
config.url = config.url || window.location.href.split('#')[0];
//当然,参数只能由地址里面了
config.data = this.toUrlParames(config.data);
config.url += config.url.indexOf('?') == -1 ? '?' + config.data : '&' + config.data;
//设置全局回调
window[callback] = function (data) {
//调用匿名函数
success(data);
//清除全局函数
window[callback] = undefined;
try {
delete window[callback];
} catch (e) { }
//确保js标签已被删除
if (head) {
head.removeChild(script);
}
}
//一个获取js的文件的方法
var head = document.getElementsByTagName("head")[0] || document.documentElement;
var script = document.createElement("script");
script.src = config.url;
var done = false;
//文件加载完毕后,处理
script.onload = script.onreadystatechange = function () {
if (!done && (!this.readyState === "loaded" || this.readyState === "complete")) {
done = true;
//加载完毕,就清除js标签
script.onload = script.onreadystatechange = null;
if (head && script.parentNode) {
head.removeChild(script);
}
}
};
//开始加载文件
head.insertBefore(script, head.firstChild);
},
//get方法
get: function (config) {
config.type = 'GET';
config.url = config.url || window.location.href.split('#')[0];
config.data = this.toUrlParames(config.data)
config.url += config.url.indexOf('?') == -1 ? '?' + config.data : '&' + config.data;
this.ajaxCore(config);
},
//post方法
post: function (config) {
config.type = 'POST';
var processData = config.processData || true, dataType = config.dataType || "";
config.url = config.url || window.location.href.split('#')[0];
if (processData) {
if (Kess.isObject(config.data)) {
if (dataType == "json") {
config.data = JSON.stringify(config.data);
}
else {
config.data = this.toUrlParames(config.data);
}
}
}
if (dataType == "json") {
config.ContentType = "application/json; charset=utf-8";
}
this.ajaxCore(config);
}

});

具体的用法:

//测试的是  asmx 功能 测试成功,建议用 post方法
Kess.ajax({
url: "webservice1.asmx/api",
data: { name: "你是大笨蛋" },
dataType: "json",
success: function (data) {
alert(data + JSON.stringify(data));
}
});
//这个是直接用post方法 简单直接,方便
Kess.post({
url: "webservice1.asmx/api",
data: { name: "你是大笨蛋" },
dataType: "json",
success: function (data) {
alert(data + JSON.stringify(data));
}
});
// jsonp 跨域正常
Kess.getJson({
url: "http://www.runoob.com/try/ajax/jsonp.php",
callname: "jsoncallback",
success: function (data) {
alert(data + JSON.stringify(data));
}
});
//表单异步上传
function doUpload() {
Kess.submit({
url: "index.ashx",
formName: "uploadForm",
data: { id: "123" },
success: function (data) {
alert(data + JSON.stringify(data));
}
});
}