Ajax Call back PHP interface return JSON data AJAX JSONP Ajax JSON instance AJAX get background JSON number

Source: Internet
Author: User
Tags call back
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.