Skip to content
The simple HTTP / RESTful requests library of JavaScript (XHR).
JavaScript HTML CSS PHP
Find file
Latest commit 83cbca6 @pandao Release v0.2.0
Failed to load latest commit information.
dist Release v0.2.0
examples Release v0.2.0
src Release v0.2.0
.gitignore Release v0.1.0
.jshintrc Release v0.1.0
CHANGE.md Release v0.2.0
Gulpfile.js Release v0.1.0
LICENSE Initial commit
README.md update README.md
bower.json Release v0.2.0
package.json Release v0.2.0

README.md

httpx.js

The simple HTTP / RESTful requests library of JavaScript (XHR).

Basic support XHR1 (XMLHttpRequest Level 1), Promise and XHR2 (XMLHttpRequest Level 2) support later.

Install

bower install httpx.js --save

Methods

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE
  • Other
  • JSON
  • JSONP
  • getScript

Compatibility

  • Firefox 4.0+
  • Chrome 7+
  • IE 9+
  • Opera 11.60+
  • Safari 5.1.4+

Examples

Description :

get|post|put|patch|delete|json(alias getJSON)(url, [data], [callback], [error]);

GET :

httpx.get("http://localhost/http-test/get.php?foo=Level1&bar=XHR&zh=中文", function(data) {
    //console.log(data, this);
}, function(method, url) {
    console.error("Custom Error", method, url, this.status, this.statusText);
});

POST :

httpx.post("http://localhost/http-test/post.php?edfd=eedfd&dfsdf=ere", {
    a : 12,
    b : "bbbb",
    c : 123489
}, function(data) {
    console.log(data, this);
});

PUT :

httpx.put("http://localhost/http-test/put.php?edfd=eedfd&dfsdf=ere", {
    a : 12,
    b : "bbbb",
    c : 123489
}, function(data) {
    console.log(data, this);
});

PATCH :

httpx.patch("http://localhost/http-test/patch.php?edfd=eedfd&dfsdf=ere", {
    a : 12,
    b : "bbbb",
    c : 123489
}, function(data) {
    console.log(data, this);
});

DELETE :

httpx.delete("http://localhost/http-test/delete.php?edfd=eedfd&dfsdf=ere", {
    a : 12,
    b : "bbbb",
    c : 123489
}, function(data) {
    console.log(data, this);
});

JSON (get json) :

// Alias getJSON(), like jQuery
httpx.json("http://localhost/http-test/get-json.php?temp="+(new Date).getTime(), {
    test : 123
}, function(json) {
    console.log("get json =>", json);
});

JSONP :

jsonp(url, [data], [callback], [callbackName]); // callbackName for query string name
httpx.jsonp("http://192.168.1.2/http-test/jsonp.php?temp="+(new Date).getTime(), {
    test : 123
}, function(json) {
    console.log("jsonp =>", json);
}, "callback");

getScript :

httpx.getScript("http://192.168.1.2/http-test/test.js", function() {
    test();
});

Options

defaults :

{
    async         : true,
    timeout       : 3000,
    method        : "GET",
    url           : "",
    data          : "",
    dataType      : "text",
    headers       : {},
    contentType   : "text/plain; charset=UTF-8",
    jsonp         : "callback",    // for query string
    success       : function() {},
    error         : function(method, url) {},
    ontimeout     : function(method, url) {}
}

usage :

httpx.request({
    url : "http://localhost/http-test/head.php",
    method : "HEAD",  // Custom http method
    headers : {},  // Custom http headers
    success : function(data) {
        console.log(data);
    }
});

httpx.get({
    url : "http://localhost/http-test/get.php?foo=bar",
    headers : {
        "xxxxxxx" : "xxxxx"
    },
    success : function(data) {
        console.log(data);
    }
});

httpx.post({
    url : "http://localhost/http-test/post.php?foo=bar",
    data : {
        id : 123,
        title : "xxxxx"
    },
    headers : {
        "xxxxxxx" : "xxxxx"
    },
    success : function(data) {
        console.log(data);
    }
});

// put/patch/delete/json ...

Changes

Change logs

License

The MIT License.

Copyright (c) 2015 Pandao

Something went wrong with that request. Please try again.