2

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.

5
  • 1
    Non-whitespace usually means there's extra non-json garbage at the end of the data, something like [1,2,3]blahblahblah. Commented Nov 1, 2011 at 19:57
  • Can you post the json that is generated? Commented Nov 1, 2011 at 22:40
  • 1
    @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 ? Commented Nov 2, 2011 at 12:12
  • @Fanny: show the json generated by the lamp server, then. Commented Nov 2, 2011 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 Commented Nov 2, 2011 at 14:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 ?
It's fairly common, try using notepad++ under the encoding section at the top, just convert it to UTF-8 without BOM.
I had to convert my controller, my view and my form, and then it worked ! Thank you =)
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 =)
1

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

5 Comments

I just tried, I validated this : {"quota": 90,"usedSpace": 73.904, "usedPercentage": "82%"} and it says valid json.
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.
No it's not public, but what file should I analyze ? The javascript function ? Sorry but I'm not very familiar with JSON.
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.
Finally my encoding was the problem, but Thank you for your answers !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.