1

$.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 の結果は変わりません。これが可能かどうかは誰にもわかりませんか?もしそうなら、どうすればよいですか?

敬具、クリス

4

1 に答える 1

0

あなたの成功ハンドラーは、あなたの成功ハンドラーを$.postオーバーライドします$.ajaxSetup。から成功ハンドラー定義を抽出する必要がありますajaxSetup

function handleSuccess(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");
        }
    }

次に、次の$.ajaxSetupようにします:

$.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 : handleSuccess,
    done    : function(){
        console.log("done");
    },
    always  : function(){
        console.log("always");
    }
});

次に、次の$.postようにします:

$.post("brands.php",{method:"composeMessage",data:{brand:brandId}},function(data){
        handleSuccess(data);
        console.log(data);
    })
于 2013-10-30T09:33:48.363 に答える