例如,当我通过ajax发布时,我有时会得到额外的字符。如果文本通过ajax (一个php $_POST ),我最终会得到:
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; });发布于 2012-04-01 01:54:29
通过一个消除过程,我发现错误是由传递给查询字符串的无效数据引起的。
The line:
data: 'do=leave_footprint&footPrint=' + footPrint + '&ref=' + goUserId + '&json=1',我注意到如果‘?’的话,footPrint变量总是会破坏脚本。已经通过了。很多议员在提出质询时会使用“?”。什么时候一个都没有?
通过将footPrint变量包装在encodeURIComponent()中,我可以将所有文本发送到encodeURIComponent脚本,而不会破坏URL字符串。
新线:
data: 'do=leave_footprint&footPrint=' + encodeURIComponent(footPrint) + '&ref=' + goUserId + '&json=1',这个解决方案对我有效..。问题、意见和建议仍然欢迎。
发布于 2012-03-24 20:56:32
尝试将缓存设置为false。来自http://api.jquery.com/jQuery.ajax/
缓存布尔默认值: true,false for dataType 'script‘和'jsonp’,如果设置为false,它将强制被请求的页面不被浏览器缓存。将缓存设置为false也会将查询字符串参数"_=TIMESTAMP“附加到URL。
https://stackoverflow.com/questions/9855005
复制相似问题