Error with JSON in javascript : unexpected non-whitespace character after JSON data

五迷三道 提交于 2019-12-08 02:46:15

问题


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.

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.

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

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

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)"

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

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. 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.


回答1:


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.




回答2:


jQuery has rather strict settings for JSON data. Have you already tried to validate your JSON, e.g. with JSONLint



来源:https://stackoverflow.com/questions/7972110/error-with-json-in-javascript-unexpected-non-whitespace-character-after-json-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!