我的问题是关于我在 javascript 函数中使用 jQuery.ajax 时遇到的错误。我搜索并找到了类似的问题,但这些问题都没有帮助我。
所以,真正奇怪的是它以前是有效的。我花了几天时间处理 UTF-8 的翻译问题,然后当我再次尝试时,它不再工作了。我看不出发生了什么,所以也许你可以帮我找出答案。
这是为 ajax 调用的 Controller 操作中的代码。
$project =$_GET['project'];
//Call private function getQuota
$quotaTab = $this->getQuota($project);
$this->_helper->getHelper('viewRenderer')->setNoRender();
Zend_Layout::getMvcInstance()->disableLayout();
// Encode data to return
$return = array(
'quota' => $quotaTab[0],
'usedSpace' => $quotaTab[1],
'usedPercentage' => $quotaTab[2]
);
$return = Zend_Json::encode($return);
$this->getResponse()->setBody($return);
这是我的包含 ajax 函数的 javascript 函数
function changeQuota() {
var fileset = document.getElementById('fileset');
jQuery.ajax({
url: '/filesets/quota/',
data: ({ project: fileset.value, ajaxCall: true}),
success: function(response) {
//Decode the JSON response
var result = response.evalJSON();
// Set the new value of current quota
$('currentQuota').value = result.quota;
var strUsed = <?php echo "' ".$this->translate("used")."'";?>;
$('usedQuota').innerHTML = result.usedPercentage.concat(strUsed);
var suggestQuota = Math.round(parseFloat(result.quota) + parseFloat(result.quota/10));
$('quota').value = suggestQuota;
$('usedSpace').value = Math.round(result.usedSpace);
}
});
}
所以我用 Firebug 调试我的脚本,它返回的确切错误是
“JSON.parse:JSON 数据后意外的非空白字符”
返回 JSON.parse(json); prototype.js(第 720 行)”
当我使用 jQuery.ajax 时,我在两个函数中都出现了这个错误。尝试 evalJSON() 时发生错误。
知道为什么它不再起作用了吗?可能是因为文件的编码(当前为 UTF-8)?
在此先感谢您的帮助!
附言也许它没有链接,但是当我用 Zend Studio 打开 prototype.js 时,我有一些警告和 2 个错误(第 4235 行的“缺少分号”和第 4000 行的“ token 语法错误”,“,.预期”。当我注意到这一点时,我下载了最新版本并且它没有改变。
最佳答案
您可以在发送前 trim 最后的字符串吗?如果不是这种情况,请确保它的编码没有字节顺序标记,特别是如果您使用的是带有 UTF-8 的 Dream Weaver。
关于php - javascript : unexpected non-whitespace character after JSON data 中的 JSON 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7972110/