Repository navigation
API document
Convert an object to a query string, for adding param to url.
AH = require('ajaxHelpers.js');
var param = AH.buildParam({
uid: 1
, posts:[
'foo'
, 'bar'
]
, test:{
foo: 'bar'
}
})
//param == "uid=1&posts=foo&posts=bar&test%5Bfoo%5D=bar"
//decodeURIComponent(param) == "uid=1&posts=foo&posts=bar&test[foo]=bar"Parse a parameter string into a object.
AH.parseParam('?a=2&b=3&a=4')
// { a: [ '2', '4' ], b: '3' }
AH.parseParam('a=3&b=&c=5')
// { a: '3', b: '', c: '5' }Parse current browser's window.location.search.
Make a JSON with Padding request, if failed, error 'REQUEST_ERROR: error when making jsonp request' will be passed on, opts format:
{
url :: String
// target url, add params if you wish to send data
, callback :: String
// this is the callback key in the param
// omit it with default 'callback'
}When a jsonp Action fired with go, the script tag are returned(will be removed from DOM after request finish).
var script = AH.jsonp({
url: 'http://jsonplaceholder.typicode.com/posts?' + Action.param({
userId: 2
})
}).go(function(data) {
console.log(script);
return console.log(data);
});Since the data are already deserialized via the Padding part of JSON with Padding, the data should be a javascript value.
Make an AJAX request, opts format:
{
url :: String
// target url, when using method without request body
// eg. 'GET', 'DELETE'
// add params if you wish to send data
, method :: String
// 'GET' | 'POST' | 'DELETE' | 'PUT' ...
, data :: String | Object | FormData
// request payload, only use this when request method support body
// String will add header: 'Content-Type', 'application/x-www-form-urlencoded'
// FormData will add header: 'Content-Type', 'multipart/form-data; boundary=...'
// Other Object will add header: 'Content-Type', 'application/json; charset=UTF-8'
, headers :: Object
// custom headers key-value pairs
, timeout :: Int
// max request time in milliseconds
, responseType :: String
// Enumerated value that defines the response type
// only works with some broswers
// check https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
}Without setting responseType, response are passed as string as it is, so deserialize it if you must, If request failed, error 'REQUEST_ERROR: statusx' will be passed on, x is the xhr status, if request didn't finish within given timeout, error 'REQUEST_ERROR: timeout' will be passed on. When ajax Action are fired, the xhr object will be returned without block.
// send data with urlencoded string
var xhr = AH.ajax({
method: 'POST',
url: 'http://jsonplaceholder.typicode.com/posts',
data: AH.buildParam({
what: 3
})
})
.next(JSON.parse) // all responses are string if no responseType provided
.go(function(data) {
// use xhr object to obtain response headers
console.log(xhr.getAllResponseHeaders());
console.log(data);
});
// send object with json
var xhr = AH.ajax({
method: 'POST',
url: 'http://jsonplaceholder.typicode.com/posts',
data: {
foo: 'bar'
},
timeout: 100
}).guard(function(e) {
console.log(e);
return 'error handled';
}).go(function(data) {
console.log(data);
});
// send FormData
var f = new FormData();
var f.append('username', 'Chris');
var xhr = AH.ajax({
method: 'POST',
url: 'http://jsonplaceholder.typicode.com/posts',
data: f,
// use responseType to auto deserialize data
responseType: 'json'
}).go(function(data) {
console.log(data);
});