Debian Codegraph @

README.md

raw symbols SCIP
1 # It's AJAX
2
3 All over again. Includes support for xmlHttpRequest, JSONP, CORS, and CommonJS Promises A.
4
5 It is also isomorphic allowing you to `require('reqwest')` 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).
6
7 ## API
8
9 ``` js
10 reqwest('path/to/html', function (resp) {
11 qwery('#content').html(resp)
12 })
13
14 reqwest({
15 url: 'path/to/html'
16 , method: 'post'
17 , data: { foo: 'bar', baz: 100 }
18 , success: function (resp) {
19 qwery('#content').html(resp)
20 }
21 })
22
23 reqwest({
24 url: 'path/to/html'
25 , method: 'get'
26 , data: [ { name: 'foo', value: 'bar' }, { name: 'baz', value: 100 } ]
27 , success: function (resp) {
28 qwery('#content').html(resp)
29 }
30 })
31
32 reqwest({
33 url: 'path/to/json'
34 , type: 'json'
35 , method: 'post'
36 , error: function (err) { }
37 , success: function (resp) {
38 qwery('#content').html(resp.content)
39 }
40 })
41
42 reqwest({
43 url: 'path/to/json'
44 , type: 'json'
45 , method: 'post'
46 , contentType: 'application/json'
47 , headers: {
48 'X-My-Custom-Header': 'SomethingImportant'
49 }
50 , error: function (err) { }
51 , success: function (resp) {
52 qwery('#content').html(resp.content)
53 }
54 })
55
56 // Uses XMLHttpRequest2 credentialled requests (cookies, HTTP basic auth) if supported
57 reqwest({
58 url: 'path/to/json'
59 , type: 'json'
60 , method: 'post'
61 , contentType: 'application/json'
62 , crossOrigin: true
63 , withCredentials: true
64 , error: function (err) { }
65 , success: function (resp) {
66 qwery('#content').html(resp.content)
67 }
68 })
69
70 reqwest({
71 url: 'path/to/data.jsonp?callback=?'
72 , type: 'jsonp'
73 , success: function (resp) {
74 qwery('#content').html(resp.content)
75 }
76 })
77
78 reqwest({
79 url: 'path/to/data.jsonp?foo=bar'
80 , type: 'jsonp'
81 , jsonpCallback: 'foo'
82 , jsonpCallbackName: 'bar'
83 , success: function (resp) {
84 qwery('#content').html(resp.content)
85 }
86 })
87
88 reqwest({
89 url: 'path/to/data.jsonp?foo=bar'
90 , type: 'jsonp'
91 , jsonpCallback: 'foo'
92 , success: function (resp) {
93 qwery('#content').html(resp.content)
94 }
95 , complete: function (resp) {
96 qwery('#hide-this').hide()
97 }
98 })
99 ```
100
101 ## Promises
102
103 ``` js
104 reqwest({
105 url: 'path/to/data.jsonp?foo=bar'
106 , type: 'jsonp'
107 , jsonpCallback: 'foo'
108 })
109 .then(function (resp) {
110 qwery('#content').html(resp.content)
111 }, function (err, msg) {
112 qwery('#errors').html(msg)
113 })
114 .always(function (resp) {
115 qwery('#hide-this').hide()
116 })
117 ```
118
119 ``` js
120 reqwest({
121 url: 'path/to/data.jsonp?foo=bar'
122 , type: 'jsonp'
123 , jsonpCallback: 'foo'
124 })
125 .then(function (resp) {
126 qwery('#content').html(resp.content)
127 })
128 .fail(function (err, msg) {
129 qwery('#errors').html(msg)
130 })
131 .always(function (resp) {
132 qwery('#hide-this').hide()
133 })
134 ```
135
136 ``` js
137 var r = reqwest({
138 url: 'path/to/data.jsonp?foo=bar'
139 , type: 'jsonp'
140 , jsonpCallback: 'foo'
141 , success: function () {
142 setTimeout(function () {
143 r
144 .then(function (resp) {
145 qwery('#content').html(resp.content)
146 }, function (err) { })
147 .always(function (resp) {
148 qwery('#hide-this').hide()
149 })
150 }, 15)
151 }
152 })
153 ```
154
155 ## Options
156
157 * `url` a fully qualified uri
158 * `method` http method (default: `GET`)
159 * `headers` http headers (default: `{}`)
160 * `data` entity body for `PATCH`, `POST` and `PUT` requests. Must be a query `String` or `JSON` object
161 * `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.
162 * `contentType` sets the `Content-Type` of the request. Eg: `application/json`
163 * `crossOrigin` for cross-origin requests for browsers that support this feature.
164 * `success` A function called when the request successfully completes
165 * `error` A function called when the request fails.
166 * `complete` A function called whether the request is a success or failure. Always called when complete.
167 * `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 reqwest.
168
169 ## Security
170
171 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
172
173 ``` html
174 <script>
175 (function () {
176 if (!window.JSON) {
177 document.write('<scr' + 'ipt src="http://cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"><\/scr' + 'ipt>')
178 }
179 }());
180 </script>
181 ```
182
183
184 ## Contributing
185
186 ``` sh
187 $ git clone git://github.com/ded/reqwest.git reqwest
188 $ cd !$
189 $ npm install
190 ```
191
192 Please keep your local edits to `src/reqwest.js`.
193 The base `./reqwest.js` and `./reqwest.min.js` will be built upon releases.
194
195 ## Running Tests
196
197 ``` sh
198 make test
199 ```
200
201 ## Browser support
202
203 * IE6+
204 * Chrome 1+
205 * Safari 3+
206 * Firefox 1+
207 * Opera
208
209 ## Ender Support
210 Reqwest can be used as an [Ender](http://enderjs.com) module. Add it to your existing build as such:
211
212 $ ender add reqwest
213
214 Use it as such:
215
216 ``` js
217 $.ajax({ ... })
218 ```
219
220 Serialize things:
221
222 ``` js
223 $(form).serialize() // returns query string -> x=y&...
224 $(form).serialize({type:'array'}) // returns array name/value pairs -> [ { name: x, value: y}, ... ]
225 $(form).serialize({type:'map'}) // returns an object representation -> { x: y, ... }
226 $(form).serializeArray()
227 $.toQueryString({
228 foo: 'bar'
229 , baz: 'thunk'
230 }) // returns query string -> foo=bar&baz=thunk
231 ```
232
233 Or, get a bit fancy:
234
235 ``` js
236 $('#myform input[name=myradios]').serialize({type:'map'})['myradios'] // get the selected value
237 $('input[type=text],#specialthing').serialize() // turn any arbitrary set of form elements into a query string
238 ```
239
240 ## ajaxSetup
241 Use the `request.ajaxSetup` to predefine a data filter on all requests. See the example below that demonstrates JSON hijacking prevention:
242
243 ``` js
244 $.ajaxSetup({
245 dataFilter: function (response, type) {
246 if (type == 'json') return response.substring('])}while(1);</x>'.length)
247 else return response
248 }
249 })
250 ```
251
252 ## RequireJs and Jam
253 Reqwest can also be used with RequireJs and can be installed via jam
254
255 ```
256 jam install reqwest
257 ```
258
259 ```js
260 define(function(require){
261 var reqwest = require('reqwest')
262 });
263 ```
264
265 ## spm
266 Reqwest can also be installed via spm [![](http://spmjs.io/badge/reqwest)](http://spmjs.io/package/reqwest)
267
268 ```
269 spm install reqwest
270 ```
271
272 ## jQuery and Zepto Compatibility
273 There are some differences between the *Reqwest way* and the
274 *jQuery/Zepto way*.
275
276 ### method ###
277 jQuery/Zepto use `type` to specify the request method while Reqwest uses
278 `method` and reserves `type` for the response data type.
279
280 ### dataType ###
281 When using jQuery/Zepto you use the `dataType` option to specify the type
282 of data to expect from the server, Reqwest uses `type`. jQuery also can
283 also take a space-separated list of data types to specify the request,
284 response and response-conversion types but Reqwest uses the `type`
285 parameter to infer the response type and leaves conversion up to you.
286
287 ### JSONP ###
288 Reqwest also takes optional `jsonpCallback` and `jsonpCallbackName`
289 options to specify the callback query-string key and the callback function
290 name respectively while jQuery uses `jsonp` and `jsonpCallback` for
291 these same options.
292
293
294 But fear not! If you must work the jQuery/Zepto way then Reqwest has
295 a wrapper that will remap these options for you:
296
297 ```js
298 reqwest.compat({
299 url: 'path/to/data.jsonp?foo=bar'
300 , dataType: 'jsonp'
301 , jsonp: 'foo'
302 , jsonpCallback: 'bar'
303 , success: function (resp) {
304 qwery('#content').html(resp.content)
305 }
306 })
307
308 // or from Ender:
309
310 $.ajax.compat({
311 ...
312 })
313 ```
314
315 If you want to install jQuery/Zepto compatibility mode as the default
316 then simply place this snippet at the top of your code:
317
318 ```js
319 $.ajax.compat && $.ender({ ajax: $.ajax.compat });
320 ```
321
322
323 **Happy Ajaxing!**