Create a gist now

Instantly share code, notes, and snippets.

@helloGoGo /http.js
Last active Mar 8, 2017

What would you like to do?
Deferredを用いたajax共通オブジェクト
var getData = function (query) {
var param = {
url: 'http://hoge',
data: {
userId : 'hoge'
query: query,
},
dataType: 'json'
}
//success
Http.get(param).done(function(data) {
console.log(data);
});
//fail
Http.get(param).fail(function(data) {
console.log('fail');
});
}
//common
var Http = {
var defer = $.Deferred();
get : function (param) {
$.ajax({
url: param.url,
data: param.data || '',
dataType: param.dataType || '',
success: defer.resolve,
error: defer.reject
});
return defer.promise();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment