reqwest

所属分类:JavaScript/JQuery
开发工具:JavaScript
文件大小:9143KB
下载次数:0
上传日期:2021-10-03 11:58:08
上 传 者sh-1993
说明:  浏览器异步http请求
(browser asynchronous http requests)

文件列表:
.jshintrc (1208, 2015-12-21)
.travis.yml (162, 2015-12-21)
Makefile (383, 2015-12-21)
build.json (1512, 2015-12-21)
make (0, 2015-12-21)
make\bump.js (274, 2015-12-21)
make\tests.js (2848, 2015-12-21)
package.json (1029, 2015-12-21)
phantom.js (936, 2015-12-21)
reqwest.js (19211, 2015-12-21)
reqwest.min.js (9780, 2015-12-21)
src (0, 2015-12-21)
src\copyright.js (135, 2015-12-21)
src\ender.js (642, 2015-12-21)
src\reqwest.js (19490, 2015-12-21)
test.js (429, 2015-12-21)
tests (0, 2015-12-21)
tests\ender.js (3251, 2015-12-21)
tests\fixtures (0, 2015-12-21)
tests\fixtures\badfixtures.xml (34, 2015-12-21)
tests\fixtures\fixtures.html (12, 2015-12-21)
tests\fixtures\fixtures.js (23, 2015-12-21)
tests\fixtures\fixtures.json (20, 2015-12-21)
tests\fixtures\fixtures.xml (33, 2015-12-21)
tests\fixtures\fixtures_jsonp.jsonp (32, 2015-12-21)
tests\fixtures\fixtures_jsonp2.jsonp (26, 2015-12-21)
tests\fixtures\fixtures_jsonp3.jsonp (33, 2015-12-21)
tests\fixtures\fixtures_jsonp_multi.jsonp (25, 2015-12-21)
tests\fixtures\fixtures_jsonp_multi_b.jsonp (25, 2015-12-21)
tests\fixtures\fixtures_jsonp_multi_c.jsonp (25, 2015-12-21)
tests\fixtures\fixtures_with_prefix.json (36, 2015-12-21)
tests\fixtures\invalidJSON.json (86, 2015-12-21)
tests\tests.html (4960, 2015-12-21)
tests\tests.js (56263, 2015-12-21)
use-me.sublime-project (463, 2015-12-21)
vendor (0, 2015-12-21)
vendor\phantomjs (9456379, 2015-12-21)
... ...

# It's AJAX All over again. Includes support for xmlHttpRequest, JSONP, CORS, and CommonJS Promises A. It is also isomorphic allowing you to `require('re***st')` in `Node.js` through the peer dependency [xhr2](https://github.com/pwnall/node-xhr2), albeit the original intent of this library is for the browser. For a more thorough solution for Node.js, see [mikeal/request](https://github.com/request/request). ## API ``` js re***st('path/to/html', function (resp) { ***ry('#content').html(resp) }) re***st({ url: 'path/to/html' , method: 'post' , data: { foo: 'bar', baz: 100 } , success: function (resp) { ***ry('#content').html(resp) } }) re***st({ url: 'path/to/html' , method: 'get' , data: [ { name: 'foo', value: 'bar' }, { name: 'baz', value: 100 } ] , success: function (resp) { ***ry('#content').html(resp) } }) re***st({ url: 'path/to/json' , type: 'json' , method: 'post' , error: function (err) { } , success: function (resp) { ***ry('#content').html(resp.content) } }) re***st({ url: 'path/to/json' , type: 'json' , method: 'post' , contentType: 'application/json' , headers: { 'X-My-Custom-Header': 'SomethingImportant' } , error: function (err) { } , success: function (resp) { ***ry('#content').html(resp.content) } }) // Uses XMLHttpRequest2 credentialled requests (cookies, HTTP basic auth) if supported re***st({ url: 'path/to/json' , type: 'json' , method: 'post' , contentType: 'application/json' , crossOrigin: true , withCredentials: true , error: function (err) { } , success: function (resp) { ***ry('#content').html(resp.content) } }) re***st({ url: 'path/to/data.jsonp?callback=?' , type: 'jsonp' , success: function (resp) { ***ry('#content').html(resp.content) } }) re***st({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' , jsonpCallbackName: 'bar' , success: function (resp) { ***ry('#content').html(resp.content) } }) re***st({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' , success: function (resp) { ***ry('#content').html(resp.content) } , complete: function (resp) { ***ry('#hide-this').hide() } }) ``` ## Promises ``` js re***st({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' }) .then(function (resp) { ***ry('#content').html(resp.content) }, function (err, msg) { ***ry('#errors').html(msg) }) .always(function (resp) { ***ry('#hide-this').hide() }) ``` ``` js re***st({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' }) .then(function (resp) { ***ry('#content').html(resp.content) }) .fail(function (err, msg) { ***ry('#errors').html(msg) }) .always(function (resp) { ***ry('#hide-this').hide() }) ``` ``` js var r = re***st({ url: 'path/to/data.jsonp?foo=bar' , type: 'jsonp' , jsonpCallback: 'foo' , success: function () { setTimeout(function () { r .then(function (resp) { ***ry('#content').html(resp.content) }, function (err) { }) .always(function (resp) { ***ry('#hide-this').hide() }) }, 15) } }) ``` ## Options * `url` a fully qualified uri * `method` http method (default: `GET`) * `headers` http headers (default: `{}`) * `data` entity body for `PATCH`, `POST` and `PUT` requests. Must be a query `String` or `JSON` object * `type` a string enum. `html`, `xml`, `json`, or `jsonp`. Default is inferred by resource extension. Eg: `.json` will set `type` to `json`. `.xml` to `xml` etc. * `contentType` sets the `Content-Type` of the request. Eg: `application/json` * `crossOrigin` for cross-origin requests for browsers that support this feature. * `success` A function called when the request successfully completes * `error` A function called when the request fails. * `complete` A function called whether the request is a success or failure. Always called when complete. * `jsonpCallback` Specify the callback function name for a `JSONP` request. This value will be used instead of the random (but recommended) name automatically generated by re***st. ## Security If you are *still* requiring support for IE6/IE7, consider including [JSON3](https://bestiejs.github.io/json3/) in your project. Or simply do the following ``` html ``` ## Contributing ``` sh $ git clone git://github.com/ded/re***st.git re***st $ cd !$ $ npm install ``` Please keep your local edits to `src/re***st.js`. The base `./re***st.js` and `./re***st.min.js` will be built upon releases. ## Running Tests ``` sh make test ``` ## Browser support * IE6+ * Chrome 1+ * Safari 3+ * Firefox 1+ * Opera ## Ender Support Re***st can be used as an [Ender](http://enderjs.com) module. Add it to your existing build as such: $ ender add re***st Use it as such: ``` js $.ajax({ ... }) ``` Serialize things: ``` js $(form).serialize() // returns query string -> x=y&... $(form).serialize({type:'array'}) // returns array name/value pairs -> [ { name: x, value: y}, ... ] $(form).serialize({type:'map'}) // returns an object representation -> { x: y, ... } $(form).serializeArray() $.toQueryString({ foo: 'bar' , baz: 'thunk' }) // returns query string -> foo=bar&baz=thunk ``` Or, get a bit fancy: ``` js $('#myform input[name=myradios]').serialize({type:'map'})['myradios'] // get the selected value $('input[type=text],#specialthing').serialize() // turn any arbitrary set of form elements into a query string ``` ## ajaxSetup Use the `request.ajaxSetup` to predefine a data filter on all requests. See the example below that demonstrates JSON hijacking prevention: ``` js $.ajaxSetup({ dataFilter: function (response, type) { if (type == 'json') return response.substring('])}while(1);'.length) else return response } }) ``` ## RequireJs and Jam Re***st can also be used with RequireJs and can be installed via jam ``` jam install re***st ``` ```js define(function(require){ var re***st = require('re***st') }); ``` ## spm Re***st can also be installed via spm [![](http://spmjs.io/badge/re***st)](http://spmjs.io/package/re***st) ``` spm install re***st ``` ## jQuery and Zepto Compatibility There are some differences between the *Re***st way* and the *jQuery/Zepto way*. ### method ### jQuery/Zepto use `type` to specify the request method while Re***st uses `method` and reserves `type` for the response data type. ### dataType ### When using jQuery/Zepto you use the `dataType` option to specify the type of data to expect from the server, Re***st uses `type`. jQuery also can also take a space-separated list of data types to specify the request, response and response-conversion types but Re***st uses the `type` parameter to infer the response type and leaves conversion up to you. ### JSONP ### Re***st also takes optional `jsonpCallback` and `jsonpCallbackName` options to specify the callback query-string key and the callback function name respectively while jQuery uses `jsonp` and `jsonpCallback` for these same options. But fear not! If you must work the jQuery/Zepto way then Re***st has a wrapper that will remap these options for you: ```js re***st.compat({ url: 'path/to/data.jsonp?foo=bar' , dataType: 'jsonp' , jsonp: 'foo' , jsonpCallback: 'bar' , success: function (resp) { ***ry('#content').html(resp.content) } }) // or from Ender: $.ajax.compat({ ... }) ``` If you want to install jQuery/Zepto compatibility mode as the default then simply place this snippet at the top of your code: ```js $.ajax.compat && $.ender({ ajax: $.ajax.compat }); ``` **Happy Ajaxing!**

近期下载者

相关文件


收藏者