2012-03-24 69 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