使用ajax发布时,jQuery会发布错误文本

时间:2012-03-24 19:52:57

标签: php jquery

当我通过ajax发帖时,我有时会发布额外的字符。如果文本通过ajax传递给php $ _POST我最终得到:

  

这是我的留言jQuery127638276487364873268_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 个答案:

答案 0 :(得分:0)

尝试将缓存设置为false。来自http://api.jquery.com/jQuery.ajax/

缓存布尔值 默认值:true,false表示dataType'script'和'jsonp' 如果设置为false,它将强制浏览器不缓存请求的页面。将cache设置为false还会将查询字符串参数“_ = [TIMESTAMP]”附加到URL。

答案 1 :(得分:0)

我通过消除过程发现错误是由无效数据传递给查询字符串引起的。

行:

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

我注意到,如果'??',footPrint变量总会破坏脚本通过了。提问时的一些成员会使用'??'什么时候而不是一个'?'

通过将footPrint var包装在encodeURIComponent()中,我可以将所有文本发送到PHP脚本,而不会破坏URL字符串。

新行:

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

此解决方案对我有用......问题意见和建议仍然受到欢迎。