2013-10-30 73 views
1

當我使用$ .post進行ajax調用時,我想要設置一些基本設置,因此我不需要在每次使用$時都將其放入代碼中。帖子。 我這樣做是這樣的:

$.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"); 
    } 
}); 

我讓我的$。員額是這樣的:

$("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的結果。 有誰知道這是否可能,如果是這樣,怎麼做?

最好的問候, 克里斯

+0

_「我要改變$ .ajaxSetup中的「brands.php」的響應,然後討論$ .post。「_ - 我不知道這應該是什麼意思。在接收之前更改響應?沒有意義。數據收到後更改數據?好的,那有什麼問題? – CBroe

+0

使用$ .post您可以提供您自己的.success功能。 我想要$ .ajaxSetup來更改發送到$ .post中的.success的數據。 – SheperdOfFire

+0

仍然不明白你的實際問題在這裏。 – CBroe

回答

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); 
    })