patternjavascriptMinor
Is there anything can be improved in this JavaScript module?
Viewed 0 times
thiscanimprovedanythingjavascriptmodulethere
Problem
I am using RequireJS JavaScript library. I have the following JavaScript module define for dataservice:
Is there anything which can be improved in this module?
define(['jquery'], function ($) {
var callApi = function (url, type, dataType, data, callback) {
$.ajax({
url: url,
type: type,
data: data,
dataType: dataType,
contentType: "application/json",
success: function (data) {
callback(data);
}
});
};
var getData = function (url, dataType, data, callback) {
callApi(url, 'GET', dataType, data, callback);
};
var postData = function (url, data, callback) {
callApi(url, 'POST', 'json', data, callback)
};
return {
getData: getData,
postData: postData
};
});Is there anything which can be improved in this module?
Solution
I really like this code.
Still, 2 observations:
Still, 2 observations:
- The code is awfully optimistic, no default error handling ?
successgetsdata,textStatus, andjqXHR. I would pass all 3 to the callback function.
Context
StackExchange Code Review Q#41570, answer score: 4
Revisions (0)
No revisions yet.