开发者

JSON format error even though echo matches with DataTables

开发者 https://www.devze.com 2023-01-25 17:01 出处:网络
I run a lengthy PHP script I was modifying to PDO formatting as opposed to crappy mysql Db calls. At the end of the script, the $output is开发者_运维百科 json encoded with:

I run a lengthy PHP script I was modifying to PDO formatting as opposed to crappy mysql Db calls. At the end of the script, the $output is开发者_运维百科 json encoded with:

echo json_encode( $output );

When I run the two scripts, the output to screen is IDENTICAL but my jquery DataTables program returns an error on the PDO encoded results and not the mysql return.

I have run regex's on the two results and they are IDENTICAL so why this is happening is beyond me.

Anybody ever had the same issue?

EDIT

DataTables error states:

DataTables warning: JSON data from server could not be parsed. This is caused by a JSON formatting error.


One method to use is to grab the JSON return from Firebug (or Webkit's inspector or whatever) and run it through http://jsonlint.com . If the JSON string is identical, then I would guess that there is something else being output (perhaps a PHP warning, or just an echo of a string somewhere) which is causing the parsing to fail. DataTables makes use of the JSON parser built into jQuery 1.4, and will give this error when the parser cannot decode the string.

If that doesn't help, are you able to give us a link to the page?


I got this problem too. Just because I use jquery 1.5.1. after I change to user 1.4.4, the problem disappear.


For all those people that come here from google, try this:

echo '('.json_encode( $output ).')';


I have the same issues, but only when using PDO with prepared statements. JSONLint validates the output, but DataTables just does not accept it. I've created my own function using getJSON to fetch JSON data from my PHP datasource, and this actually works. Additionally I've created a duplicate datasource in which I am using mysqli, and when viewing the output in my browser, and running a compare on the 2 outputs in notepad++, both are identical.


I had exactly the same error and used a validator to make sure that my format was valid using this: http://jsonformatter.curiousconcept.com/

It was valid and it just turns out that my PHP script was "echoing" other data due to a stupid mistake of mine.

Try calling the script that generates the JSON in your browser directly to see if other things are being displayed, messing up the JSON code that your dataTable receives.

0

精彩评论

暂无评论...
验证码 换一张
取 消