Giter Site home page Giter Site logo

saber-ajax's Introduction

saber-ajax Build Status

适用于移动端、promise风格的ajax封装,支持XMLHttpRequest2

吾王之名~

Installation

通过 edp 引入模块:

$ edp import saber-ajax

Usage

var ajax = require('saber-ajax');

var request = ajax.get(url);

request.then(
    // 请求完成
    // data为responseText
    function (data) {
        renderData(data);
    },

    // 请求失败
    // error参数可能为以下四种情况
    // * 请求超时: 'timeout'
    // * 请求中止: 'abort'
    // * 未知错误: 'error'
    // * 其它情况: HTTP Status Code
    function (error) {
        showError(error);
    }
);

API

Methods

get(url[, query])

发起异步GET请求

  • url {string} 请求地址
  • query {Object=} 查询条件,会自动进行encodeURIComponent处理
  • return {Requester} 请求对象Requester

post(url[, data])

发起异步POST请求

  • url {string} 请求地址
  • data {Object|string=} 请求数据,可选。使用{Object}类型时请注意:暂时只支持单一层级序列化,不支持多层级(比如{date: {begin: '2012', end: '2013'}}
  • return {Requester} 请求对象Requester

request(url[, options])

发起请求,如果不做设置默认为GET异步请求

  • url {string} 请求地址
  • options {Object=} 配置参数
    • method {string=} 请求方式,默认为'GET'
    • data {string|Object=} 请求参数,支持FormData
    • stringify {boolean=} 是否自动序列化请求参数,默认为true
    • async {boolean=} 是否异步请求,默认为true
    • headers {Object=} 需要额外设置的请求头
    • timeout {number=) 请求超时时间,单位ms,注意 只有异步请求才有效
    • username {string=} 用户名
    • password {string=} 密码
    • responseType {string=} 返回的数据类型,默认为空。textarraybuffer各浏览器的支持力度较好,其它选项使用前请多多思量~

on(event, fn)

注册全局事件

  • event {string} 事件名称,具体支持的事件请参照事件说明
  • fn {Function} 事件处理函数

可以通过注册全局事件配合requester.handleFail来提供默认的请求失败处理,比如:

var ajax = require('saber-ajax');

// 注册全局失败事件
ajax.on('fail', function (req, error) {
    // 如果当前的失败请求没有被处理过
    // 则显示默认的错误提示
    if (!req.handleFail) {
        alert('亲~请求失败啦');
    }
});

Events

success

全局请求成功事件,任意请求成功时触发

  • req {Requester} 请求对象Requester
  • data {*} 请求返回的内容

fail

全局请求失败事件,任意请求失败时触发

  • req {Requester} 请求对象Requester
  • error {*} 错误信息(参考request.then关于错误信息的描述)

Classes

  • Requester请求对象,ajax.getajax.postajax.request的返回参数,对XMLHttpReqeust的封装,实现了Promise接口

Test

启动测试服务器

$ node test/server.js

默认端口为8848,可以通过参数修改:

$ node test/server.js 8080

访问http://localhost:8848/test/runner.html

saber-ajax's People

Contributors

firede avatar liuxuanzy avatar treelite avatar

Watchers

 avatar

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.