首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery在使用ajax发布时会发布错误文本

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

Stack Overflow用户
提问于 2012-03-24 19:52:56
回答 2查看 149关注 0票数 0

例如,当我通过ajax发布时,我有时会得到额外的字符。如果文本通过ajax (一个php $_POST ),我最终会得到:

messagejQuery127638276487364873268_374632874687326487这是我的

99%的帖子都能通过.我不知道如何捕获和删除这个错误,因为它只会在某些时候发生。

代码语言:javascript
复制
// 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; });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-01 01:54:29

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

The line:

代码语言:javascript
复制
data: 'do=leave_footprint&footPrint=' + footPrint + '&ref=' + goUserId + '&json=1',

我注意到如果‘?’的话,footPrint变量总是会破坏脚本。已经通过了。很多议员在提出质询时会使用“?”。什么时候一个都没有?

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

新线:

代码语言:javascript
复制
 data: 'do=leave_footprint&footPrint=' + encodeURIComponent(footPrint) + '&ref=' + goUserId + '&json=1',

这个解决方案对我有效..。问题、意见和建议仍然欢迎。

票数 0
EN

Stack Overflow用户

发布于 2012-03-24 20:56:32

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

缓存布尔默认值: true,false for dataType 'script‘和'jsonp’,如果设置为false,它将强制被请求的页面不被浏览器缓存。将缓存设置为false也会将查询字符串参数"_=TIMESTAMP“附加到URL。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9855005

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档