$.post で ajax 呼び出しを行うとき、いくつかの基本設定を既に設定しておきたいので、$.post を使用するたびにコードに入れる必要がありません。私はこれを次のようにしました:
$.ajaxSetup({
dataType :"json", // all requests should respond with json string by default
type : "POST", // all request should POST by default
beforeSend : function(){
this.url = basepath+"include/ajax/"+this.url; // before making the request, change url location to ajax folder
},
error : function(xhr, textStatus, errorThrown){
displayMessage("error", "Request could not be completed: <pre>"+errorThrown+"</pre>", true);
},
success : function(event){
console.log(event);
if(event.responseJSON !== undefined && event.responseJSON.status !== undefined && event.responseJSON.status === true){
if(event.responseJSON.output === undefined){
console.log("something is wrong 1");
displayMessage("error", "Property output was not found in response.");
}
else{
event.responseJSON.status = false;
console.log("something is wrong 2");
}
}
else{
console.log("something is wrong 3");
}
},
done : function(){
console.log("done");
},
always : function(){
console.log("always");
}
});
$.post を次のように作成します。
$("div#brandsView button.compose").click(function(e){
$.post("brands.php",{method:"composeMessage",data:{brand:brandId}},function(data){
console.log(data)
})
});
$.ajaxSetup の "brands.php" からの応答を、$.post に到達する前に変更したいという問題が発生しました。ご覧のとおり、success や complete などの関数を試してみましたが、$.post の結果は変わりません。これが可能かどうかは誰にもわかりませんか?もしそうなら、どうすればよいですか?
敬具、クリス