微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

httpx.js HTTP/RESTful 请求库

程序名称:httpx.js

授权协议: MIT

操作系统: 跨平台

开发语言: JavaScript

httpx.js 介绍

httpx.js 是一个简单的 JavaScript HTTP/RESTful 请求库。

主要方法

  • GET

  • POST

  • PUT

  • PATCH

  • DELETE

  • Other

  • JSON

  • JSONP

  • getScript

兼容情况

  • Firefox 4.0+

  • Chrome 7+

  • IE 9+

  • Opera 11.60+

  • Safari 5.1.4+

使用方法

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);
});

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(getJSON):

// 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();
});

选项

{
    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) {}
}


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

httpx.js 官网

https://github.com/pandao/httpx.js

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐