2012-03-24 93 views
0

當我通過ajax發佈我有時會得到額外的字符發佈例如。如果文本通過AJAX雖然喲PHP $ _POST我最終得到:jquery發佈錯誤文本時,發佈與ajax

這是我messagejQuery127638276487364873268_374632874687326487的職位雖然經過精細的時間

99%......我不知道該如何捕獲並移除此錯誤,因爲它只發生在某些時間。

// this is the ajax that we need to post the footprint to the wall. 
$('#submitbutton').click(function() { 
    var footPrint = $('#footPrint').val(); 
      var goUserId = '1'; 
    $.ajax({ 
     type: 'POST', 
     url: '/scripts/ajax-ProfileObjects.php', 
     data: 'do=leave_footprint&footPrint=' + footPrint + '&ref=' + goUserId + '&json=1', 
     dataType: 'json', 
     success: function(data){ 

      var textError = data.error; 
      var textAction = data.action; 
      var textList = data.list; 

      if (textError == 'post_twice' || textError =='footprint_empty' || textError == 'login_req') { 
      // display the error. 

      } else { 
      // lets fade out the item and update the page. 

      } 

    }); 

return false; }); 
+2

您需要發佈您的代碼或示例才能獲得答案。 – j08691 2012-03-24 19:54:54

回答

0

我通過排除過程發現錯誤是由傳遞給查詢字符串的無效數據導致的。

行:

data: 'do=leave_footprint&footPrint=' + footPrint + '&ref=' + goUserId + '&json=1', 

我注意到足跡變量總是會打破,如果腳本「?」通過了。許多成員在提問時會使用'??'何時而不是一個'?'

通過在encodeURIComponent()中包裝footPrint var,我可以將所有文本發送到PHP腳本而不會破壞URL字符串。

新線:

data: 'do=leave_footprint&footPrint=' + encodeURIComponent(footPrint) + '&ref=' + goUserId + '&json=1', 

該解決方案爲我工作...問題的意見和建議仍然歡迎。

0

嘗試設置緩存爲false。從http://api.jquery.com/jQuery.ajax/

緩存布爾 默認值:true,false爲的dataType「腳本」和「JSONP」 如果設置爲false,這將迫使要求不被瀏覽器緩存的頁面。將緩存設置爲false還會將查詢字符串參數「_ = [TIMESTAMP]」附加到URL。

+0

謝謝,我已更新我的代碼,並會看到是否有任何帖子出現。如果它是一個緩存問題,我應該也設置PHP文件沒有緩存以及? – Philip 2012-03-25 01:41:32

+0

@Philip我想你不需要把任何PHP頭()的工作人員。 _ = [TIMESTAMP]參數用於此目的 – dotoree 2012-03-25 14:27:34