Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ajax({
url:"", //添加自己的接口链接
timeOut:5000,
before:function(){
console.log("before");
console.log("before");
},
success:function(str){
console.log(str);
Expand All @@ -30,6 +30,7 @@ ajax({
| dataType | "" | 请求的类型 | jsonp |
| async | true | 是否异步 | blooean |
| timeOut | undefined | 超时时间 | number |
| callbackName | "" | JSONP的callback函数名称 | string |
| before | function(){} | 发送之前执行的函数 | function |
| error | function(){} | 请求报错执行的函数 | function |
| success | function(){} | 请求成功的回调函数 | function |
Expand Down
35 changes: 25 additions & 10 deletions ajax.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(function(window,undefined) {
// 定义Ajax类
function ajax(options) {

//编码数据
Expand All @@ -25,12 +26,16 @@
}
} else if (Object.prototype.toString.call(data) == '[object Object]') {
for (var key in data) {
value = data[key];
arr = arr.concat(encodeData(key, value, parentName));
if (data.hasOwnProperty(key)) { // 排除原型中的属性
if(typeof data[key] !== 'function') { // 排除函数类型的属性
value = data[key];
arr = arr.concat(encodeData(key, value, parentName));
}
}
}
}
return arr;
};
}
//设置字符串的遍码,字符串的格式为:a=1&b=2;
function setStrData(data) {
var arr = data.split("&");
Expand All @@ -48,7 +53,7 @@
} else if (typeof data === "object") {
data = setObjData(data);
}
data = data.join("&").replace("/%20/g", "+");
data = data.join("&").replace(/%20/g, "+");
//若是使用get方法或JSONP,则手动添加到URL中
if (type === "get" || dataType === "jsonp") {
url += url.indexOf("?") > -1 ? (url.indexOf("=") > -1 ? "&" + data : data) : "?" + data;
Expand All @@ -59,13 +64,13 @@
function createJsonp() {
var script = document.createElement("script"),
timeName = new Date().getTime() + Math.round(Math.random() * 1000),
callback = "JSONP_" + timeName;
callback = options.callbackName ? options.callbackName : ("JSONP_" + timeName); // 如果没有提供JSONP的callback名称,则使用随机名称

window[callback] = function(data) {
clearTimeout(timeout_flag);
document.body.removeChild(script);
success(data);
}
};
script.src = url + (url.indexOf("?") > -1 ? "&" : "?") + "callback=" + callback;
script.type = "text/javascript";
document.body.appendChild(script);
Expand Down Expand Up @@ -129,10 +134,19 @@
clearTimeout(timeout_flag);
}
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {

success(xhr.responseText);
// 判断响应数据的MIME类型,采用对应的解码方式
var type = xhr.getResponseHeader("Content-Type");
if (type.indexOf("xml") != -1 && xhr.responseXML) {
success(xhr.responseXML);
} else if (type.match(/^application\/json/)) {
success(JSON.parse(xhr.responseText));
} else {
success(xhr.responseText);
}
} else {
error(xhr.status, xhr.statusText);
// 将原始数据也传入
// 最后一个参数只针对JSON数据类型有效
error(xhr.status, xhr.statusText, JSON.parse(xhr.responseText));
}
}
};
Expand All @@ -148,7 +162,8 @@
contentType = options.contentType || "", //请求头
dataType = options.dataType || "", //请求的类型
async = options.async === undefined ? true : options.async, //是否异步,默认为true.
timeOut = options.timeOut, //超时时间。
timeOut = options.timeOut, //超时时间。
callbackName = options.callbackName || "", // 自定义JSONP的callback名称
before = options.before || function() {}, //发送之前执行的函数
error = options.error || function() {}, //错误执行的函数
success = options.success || function() {}; //请求成功的回调函数
Expand Down
2 changes: 1 addition & 1 deletion ajax.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var gulp = require('gulp');
var minify = require('gulp-uglify');
var rename = require('gulp-rename');
var src = './ajax.js';
var dist = './';
gulp.task('default', () => {
gulp.src(src)
.pipe(minify({
ie8: true
}))
.pipe(rename({
basename: 'ajax',
suffix: '.min'
}))
.pipe(gulp.dest(dist))
})
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "ajax",
"version": "1.0.0",
"description": "a simple ajax library",
"main": "ajax.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/gaoshu883/ajax.git"
},
"keywords": [
"ajax"
],
"author": "",
"license": "MIT",
"bugs": {
"url": "https://github.com/gaoshu883/ajax/issues"
},
"homepage": "https://github.com/gaoshu883/ajax#readme",
"devDependencies": {
"gulp": "^3.9.1",
"gulp-rename": "^1.3.0",
"gulp-uglify": "^3.0.0"
}
}