The PHP code is as follows:
Header' Content-type:application/json '); Header' Content-type:text/html;charset=utf-8 ');$email= $_get[' Email '];$user= [];$conn= @Mysql_connect ("localhost","Test","123456")or die("Failed in connecting database"); mysql_select_db ("Test",$conn); mysql_query ("Set names ' UTF-8 '");$query= the Select*From userinformation where email = ' ".$email."'";$result= mysql_query ($query);if(NULL== ($row= Mysql_fetch_array ($result))){Echo$_get[' Callback ']."(no such user)";} Else{$user [' Email '] = $email;$user [' nickname '] = $row [' nickname '];$user [' Portrait '] = $row [' Portrait '];Echo$_get[' Callback ']."(".Json_encode ($user).")";}?>
The JS code is as follows:
Two questions were encountered:
1. First question:
Uncaught syntaxerror:unexpected Token:
The solution is as follows:
This have just happened to me, and the reason is none of the reasons above. I was using the JQuery command Getjson and adding callback=?
to use JSONP (as I needed to go Cross-domain), and returning the J SON code and {"foo":"bar"}
getting the error.
This is because I should has included the callback data, something likejQuery17209314005577471107_1335958194322({"foo":"bar"})
Here are the PHP code I used to achieve this, and which degrades if JSON (without a callback) is used:
$ret [ ' foo ' ] = "Bar" ; Finish (); function Finish () { header ( "Content-type:application/json" ); if ( $_get [ ' callback ' ] { print $_get [ ' callback ' ]. "(" ; } print json_encode ( $GLOBALS [ ' ret ' ]); if ( $_get [ ' callback ' ] { print ")" ; } exit ; }
Hopefully that'll help someone in the future.
2. The second question:
Parse the JSON data. As you can see from the JavaScript above, I'm not using Jquery.parsejson () to get started with these methods, but I always report
Vm219:1 uncaught syntaxerror:unexpected token o in JSON at position 1
Error, and then not Jquery.parsejson () This method, but everything is normal. I don't know why.
The above describes the Ajax call back to the PHP interface to return JSON data, including the ajax,json aspects of the content, I hope to be interested in PHP tutorial friends helpful.