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。如您所见,我尝试了成功和完成之类的功能,但这些不会改变 $.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 回答