Last active
March 8, 2017 07:04
-
-
Save helloGoGo/fa7f967dcd9d8084f59eb7b246732065 to your computer and use it in GitHub Desktop.
Deferredを用いたajax共通オブジェクト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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