Giter Site home page Giter Site logo

ajax's Introduction

ajax

独立的ajax库,可单独使用,支持cors/jsonp等跨域请求,支持全局http拦截器、取消请求

demo

git clone https://github.com/rwson/ajax

cd ajax/demo/example

npm install

node index.js

//  开一个新的Terminal Tab,切换到server2 目录下,启动服务,模拟跨域请求

cd ../server2

npm install

node index

//	访问http://localhost:3000

Usage

//  common script tag
<script src="path/to/ajax.js"></script>
<script>
    
    //  ...
    
    ajax({
        //  ...
    });
    
    //  ...
    
</script>

//  require.js
require(["ajax", ....], function(ajax, ...) {
    
    //  ...
    
    ajax({
        //  ...
    });
    
    //  ...
    
});

在ajax/example/example.js文件中可以看到完整的调用例子

API

属性名 含义 类型 默认 是否必须
url 请求地址 String 当前页面的地址
method 数据发送方式 String "GET"
data 要传递给后端的数据 Object {}
headers 请求头设置 Object {}
dataType 后端返回的数据类型 String "json"
withCredentials 是否允许跨域请求(如果设置了cookie,可以带cookie证书) Boolean false
timeout 超时时间(ms,为-1的时候表示不设置超时时间) Number -1
async 是否异步 Boolean true
context 回调函数中的this指向 Object window
success 服务端响应成功后的回调 Function N/A
error 服务端响应失败后的回调 Function N/A
abort 服务端响应超时后的回调,设置了有效的timeout才有效 Function N/A
  • dataType的可选值有"json"、 "text"、"script"、"xml"、"jsonp"五个可选值
回调函数参数
//  success
//  当dataType为"jsonp"时,xhr无效
//  其他情况,xhr为XMLHttpRequest实例
success: function(res, xhr) {
}

//  error
//  此回调在dataType非"jsonp"情况下有效
error: function(res, ex, xhr) {
}

//  abort
//  当dataType为"jsonp"时,xhr无效
//  其他情况,xhr为XMLHttpRequest实例
abort: function(xhr) {}
全局拦截器配置
//	请求拦截器
ajax.requestIntercept([
  	function(xhr) {
      	document.querySelector("#loading").style.display = "block";
  	},
  	function(xhr) {
  	}
]);
或者
ajax.requestIntercept(function(xhr) {});

//	响应拦截器
ajax.responseIntercept([
  	function(xhr) {
      	document.querySelector("#loading").style.display = "none";
  	},
  	function(xhr) {
  	}
]);
或者
ajax.responseIntercept(function(xhr) {});

//	xhr为XMLHttpRequest实例
//  当dataType为"jsonp"时,全局拦截器无效
中途取消请求
var instance = ajax({
  //  ...
});
//	取消请求(在此处取消请求不会触发配置项中的abort回调)
instance.abord();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.