Take the 2-minute tour ×

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


// Encode data to return
$return = array(
    'quota' => $quotaTab[0],
    'usedSpace' => $quotaTab[1],
    'usedPercentage' => $quotaTab[2]
$return = Zend_Json::encode($return);


And this is my javascript function containing the ajax function

function  changeQuota() {

var fileset = document.getElementById('fileset'); 

     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.

share|improve this question
Non-whitespace usually means there's extra non-json garbage at the end of the data, something like [1,2,3]blahblahblah. –  Marc B Nov 1 '11 at 19:57
Can you post the json that is generated? –  vascowhite Nov 1 '11 at 22:40
@MarcB, @vascowhite, when I debug in local, it seems that I have garbage after my returned json, like warning messages, but they don't appear when I'm on my lampp server. Here's the content of response in my javascript function : "{"quota":90,"usedSpace":73.904,"usedPercentage":"82%"}" . Does it seems right ? –  Fanny Nov 2 '11 at 12:12
@Fanny: show the json generated by the lamp server, then. –  Marc B Nov 2 '11 at 14:20
This is what I posted, when I execute my application on my lampp server, I debug my javascript function with FireBug and this is the content of response –  Fanny Nov 2 '11 at 14:41
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
Which final string ? For the encoding, i'm not sure it's without BOM, but I don't use Dreamweaver, only Zend Studio and it the default encoding is UTF8. There is only UTF8 and no UTF8 without BOM, could it be the problem ? –  Fanny Nov 2 '11 at 12:20
It's fairly common, try using notepad++ under the encoding section at the top, just convert it to UTF-8 without BOM. –  Marc DiMillo Nov 2 '11 at 23:07
I had to convert my controller, my view and my form, and then it worked ! Thank you =) –  Fanny Nov 3 '11 at 13:26
Glad it worked out for you! :D (vote up my answer) hahaha –  Marc DiMillo Nov 3 '11 at 22:27
I can't, I only have 10 reputation and I need 15 to vote it up. As soon as I got 15, I'll come back to vote =) –  Fanny Nov 4 '11 at 11:23
add comment

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

share|improve this answer
I just tried, I validated this : {"quota": 90,"usedSpace": 73.904, "usedPercentage": "82%"} and it says valid json. –  Fanny Nov 2 '11 at 12:17
is this public, so we can take a look? If not try to use curl or wget to download the whole file and analyze it. –  topek Nov 2 '11 at 12:59
No it's not public, but what file should I analyze ? The javascript function ? Sorry but I'm not very familiar with JSON. –  Fanny Nov 2 '11 at 13:41
You can use wget to download the file exactly as the browser would. Then you can open the file in your favorite editor and see, if there are any characters that should not be there. It's a bit hard to give a better advice, without access to the ressource. –  topek Nov 2 '11 at 20:06
Finally my encoding was the problem, but Thank you for your answers ! –  Fanny Nov 3 '11 at 13:27
add comment

Your Answer


