A tool that can perform asynchronous HTTP requests (Ajax)
that will work in both React , and Angular?I usually use:
Wardar
"Promise-based HTTP client for browser and node.js" [github page] , [npm page]
Comparison of use:
"... Angular s $http "
~ javascript - JK
axios XMLHttpRequest
let http = new XMLHttpRequest();
http.open("POST", 'http://readyourlevel.jamesknelson.com/api/grade', true);
http.setRequestHeader('Content-type', 'text/html; charset=UTF-8');
http.onreadystatechange = function() {
if (http.readyState == 4) {
if (http.status == 200)
onGradeResult(JSON.parse(http.responseText));
else
onGradeError(http);
}
};
http.onerror = onGradeError;
http.send(text);
axios
axios({
url: 'http://readyourlevel.jamesknelson.com/api/grade',
method: 'post',
headers: {'Content-type': 'text/html; charset=UTF-8'},
data: text
}).then(onGradeResult, onGradeError)
Angular :
axios Angular ()
$http.post("http://example.appspot.com/rest/app", {"foo":"bar"})
.success(function(data, status, headers, config) {
$scope.data = data;
}).error(function(data, status, headers, config) {
$scope.status = status;
});
axios ( Angular React)
axios.post('http://example.appspot.com/rest/app', { foo: 'bar' })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});