将来他の人に役立つかもしれない場合に備えて、ajaxリクエストの簡単なラッパーを共有したいと思います。ほとんどの場合、同じjQueryAjax構成を関数に渡すことができます。(しかし、それは完全な変換ではありません)。
function sendAjax(ajaxConfig) {
var request = Request({
url: ajaxConfig.url,
contentType: ajaxConfig.contentType,
content: ajaxConfig.data,
headers: ajaxConfig.headers,
onComplete: function(response){
var data = response.json;
if(!data)
data = response.text;
//console.log("Ajax complete", response.status, ajaxConfig.url, response.text);
if(ajaxConfig.complete){
ajaxConfig.complete(data);
}
if(response.status >= 400){ //got error
if(ajaxConfig.error){
//console.log("Ajax error", response.status, ajaxConfig.url, response.text);
ajaxConfig.error(data);
}
}
else{ //success
if(ajaxConfig.success){
//console.log("Ajax success", ajaxConfig.url, response.text);
ajaxConfig.success(data);
}
}
}
});
switch(ajaxConfig.type){
case "POST":
request.post();
break;
case "PUT":
request.put();
case "DELETE":
request.delete();
default:
request.get();
}
}