Git Product home page Git Product logo

ajax's Introduction

GitHub release

GitHub tag

ajax

A pure javascript libary handle ajax behaivor

Usage

ajax({
    method: 'POST',
    url: 'json.php',
    data: {
        user: 'TED'
    },
    response: 'json',
    success: function (data) {
        console.log(data);
    },
    error: function(status, data) {
        // status = http status
        // do something
    }
});
// or
var ajaxData = new ajax({
    method: 'POST',
    url: 'json.php',
    data: {
        user: 'TED'
    },
    response: 'json',
    success: function (data) {
        console.log(data);
    },
    error: function(status, data) {
        // status = http status
        // do something
    }
});

And some example in index.html

Feature

  • Handle CORS with cookie(withCredentials) (IE 10+)

  • Handle IE8 CORS(XDomainRequest)

  • Handle response XML format

  • Not handle IE8 IE9 CORS with cookie(Native browser not support)

  • Support set header

    • If you want to check ajax in server side

    • you can use code like xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

  • Support Browser

    • Chrome

    • Firefox

    • Safari

    • IE8+

    • Microsoft Edge

Arguments

It is json type object

key type required value description example
method string required 'POST', 'GET', 'DELETE', 'PUT', 'OPTION', 'PATH' http request method 'GET'
url string required 'url' url you request 'json.php'
data object no {user: 'ted', age: '12'} the arguments want request {user: 'ted', age: '12'}
body string no 'form', 'json' request body format, default is form & form is use x-www-form-urlencoded, json use json 'json'
withCredentials boolean no true, false cross domain request with cookie, use it true
setRequestHeader object no {'X-Requested-With': 'XMLHttpRequest'} ajax with custom header {'X-Requested-With': 'XMLHttpRequest','X-Token': 'HAHA'}
response string no 'json', 'xml' response type is JSON or XML, default no set is JSON 'xml'
success function required function(data) {console.log(data);} handle response success function(data) {console.log(data);}
error function required function(data) {console.log(data);} handle response error function(data) {console.log(data);}

ajax's People

Contributors

tedshd avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.