使用JSON在JavaScript错误:在JSON数据意外非空白字符字符、空白、意外、错误

2023-09-10 18:17:29 作者:涼

我的问题是关于一个错误的我,当我在我的JavaScript函数中使用jQuery.ajax。我搜索,发现类似的问题,但这些问题的帮助了我。

My question is about an error I have when I use jQuery.ajax in my javascript function. I searched and found similar question, but none of these questions helped me.

那么,什么是真正奇怪的是,这是工作之前。我花了几天工作的翻译问题,UTF-8,当我之后再次试了一下,这是工作了。我看不到发生了什么,所以也许你可以帮我了解一下。

So, what's really weird is that it was working before. I spent a few days working on translations problems with UTF-8 and when I tried it again after, it was working anymore. I can't see what happened so maybe you can help me find out.

下面是我的控制器行动的code这就是所谓的AJAX。

Here is the code from my controller action which is called for ajax.

$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功能

And this is my javascript function containing the ajax function

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);
         }
      });       
}

所以,我调试我与萤火虫脚本,它返回是确切的错误 JSON.parse:意外非空格后的JSON数据字符 返回JSON.parse(JSON);的prototype.js(行720)

So I debug my script with Firebug, and the exact error that it returns is "JSON.parse: unexpected non-whitespace character after JSON data return JSON.parse(json); prototype.js (line 720)"

我在当我使用jQuery.ajax两个功能此错误。试图evalJSON()时,错误发生。

I have this error in both functions when I use jQuery.ajax. The error happens when trying to evalJSON().

任何想法,为什么它不工作了吗?难道是因为这些文件的编码(目前为UTF-8)的?

Any idea why it doesn't work anymore ? Could it be because of the encoding(currently UTF-8) of the files ?

在此先感谢您的帮助!

Thanks in advance for your help !

P.S。也许它没有联系,但是当我打开的prototype.js与Zend Studio的,我在4000行一些警告和2个错误(在行4235和语法错误令牌,缺少分号,。预计。当我发现,我下载了最新版本,并没有改变。

P.S. Maybe it's not linked, but when I open prototype.js with Zend Studio, I have some warnings and 2 errors("missing semi-colon" at line 4235 and "Syntax error on token ",", . expected" at line 4000. When I noticed that, I downloaded latest version and it didn't change.

推荐答案

您可以修剪,最后一个字符串,然后才发出去?如果不是这样确保它的连接没有字节顺序标记codeD,特别是如果你使用的梦想韦弗使用UTF-8。

You could trim the final string before it is sent out? If that isn't the case make sure it's encoded without Byte Order Mark, especially if you're using dream weaver with UTF-8.