http
http http utility
Author: harttleyangjun14@baidu.com
- http
- .ajax(url, settings) ⇒
Promise
- .get(url, data) ⇒
Promise
- .post(url, data) ⇒
Promise
- .put(url, data) ⇒
Promise
- .delete(url, data) ⇒
Promise
- .ajax(url, settings) ⇒
http.ajax(url, settings) ⇒ Promise
Perform an asynchronous HTTP (Ajax) request.
Kind: static method of http
Returns: Promise
- A promise resolves/rejects with the xhr
Param | Type | Description |
---|---|---|
url | string |
A string containing the URL to which the request is sent. |
settings | Object |
A set of key/value pairs that configure the Ajax request. All settings are optional. |
settings.method | string |
The method to open the Ajax request. |
settings.data | any |
The data to send, could be a string, a FormData, or a plain object. The 'Content-type' header is guessed accordingly, ie. 'application/x-www-form-urlencoded', 'multipart/form-data'. |
settings.headers | Object |
A set of key/value pairs that configure the Ajax request headers. If set to 'application/json', the settings.data will be JSON.stringified; If set to 'x-www-form-urlencoded', which is by default, the settings.data will be url-encoded. |
settings.jsonp | boolean |
[NOT implemented yet] Whether or not to use JSONP, default to false . |
settings.jsonpCallback | string |
[NOT implemented yet] Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by default. |
settings.xhrFields | xhrFields |
A set of key/value pairs that configure the fields of the xhr object. Note: onreadystatechange not supported, make use of the returned promise instead. |
Example
ajax('/foo', {
method: 'POST',
headers: {
"content-type": "application/json"
},
xhrFields: {
withCredentials: true
}
})
.then(function(xhr) {
xhr.status == 200;
xhr.responseHeaders['Content-Type'] == 'application/json';
xhr.responseText == '{"foo": "bar"}';
// xhr.data is parsed from responseText according to Content-Type
xhr.data === {foo: 'bar'};
});
.catch(function(xhr|errorThrown ) {});
.finally(function(xhr|errorThrown ) { });
http.get(url, data) ⇒ Promise
Load data from the server using a HTTP GET request.
Kind: static method of http
Returns: Promise
- A promise resolves/rejects with the xhr
Param | Type | Description |
---|---|---|
url | string |
A string containing the URL to which the request is sent. |
data | Object |
A plain object or string that is sent to the server with the request. |
http.post(url, data) ⇒ Promise
Load data from the server using a HTTP POST request.
Kind: static method of http
Returns: Promise
- A promise resolves/rejects with the xhr
Param | Type | Description |
---|---|---|
url | string |
A string containing the URL to which the request is sent. |
data | Object |
A plain object or string that is sent to the server with the request. |
http.put(url, data) ⇒ Promise
Load data from the server using a HTTP PUT request.
Kind: static method of http
Returns: Promise
- A promise resolves/rejects with the xhr
Param | Type | Description |
---|---|---|
url | string |
A string containing the URL to which the request is sent. |
data | Object |
A plain object or string that is sent to the server with the request. |
http.delete(url, data) ⇒ Promise
Load data from the server using a HTTP DELETE request.
Kind: static method of http
Returns: Promise
- A promise resolves/rejects with the xhr
Param | Type | Description |
---|---|---|
url | string |
A string containing the URL to which the request is sent. |
data | Object |
A plain object or string that is sent to the server with the request. |