geekandpirate

Welcome to my blog. GeekAndPirate is all about the web programming tricks,tools and techniques that I will be sharing with you. I will be also sharing all the tricks and techniques of hacking stuff(yeah that's true!!) I would be also interested in talking about the latest developments about the world of the WEB. So stick with me!

Tag Archives: ob_clean

missing ) in parenthetical

Today, while working on one of my project, I got this javascript error .

missing ) in parenthetical

This error comes due to incorrectly formatted json string.

I was trying out an ajax based image upload jquery based lib. I was using json as a response type from my server. I also noted that this error was coming while uploading only one specific image, the rest of images were working quite nicely.
I was quite confused about this error and was wondering what is wrong with this specific image. What I found out is that the image was corrupted, so my server (php) script was throwing some php errors. As I was using json as my response type, it was not properly formatting the json response, creating some invalid json string, which in return was causing error on client side (js) .
My solution was to remove any php error so that json will be formated properly. My solution was like
if($error_while_uploading){
ob_clean();
$data = array( “status” => “error”);
echo json_encode($data);
}
The php ob_clean() function cleans the output buffer. So any potential php error which might have occurred during  file resizing will get clean out and the json format will remain valid and intact.
So remember. If you see this missing ) in parenthetical error in your firebug, this is due to you got some badly formatted json string!