Example of JSONP usage under PHP

Source: Internet
Author: User
Tags setcookie

Reference: http://www.cnblogs.com/chopper/archive/2012/03/24/2403945.html

Test dependency conditions:
Project Eetopinofficial binding www.eetopin.com domain name in hosts
Project SNS binds sns.com domain name in hosts
Test code file and Source:
eetopinofficial/testcookie.php

Java code
  1. <?php
  2. Header (' Content-type:application/json ');
  3. Get Callback function name
  4. $callback = $_get [' callback '];
  5. $value = $_get[' name '];
  6. Setcookie ("name", "", Time ()-3600);
  7. Setcookie (' name ', $value, Time () +3600*24*, '/','. eetopin.com ', $_server[' Server_ PORT ']==443?   1:0);
  8. $json = "{\" name\ ": \" Add cookie success $callback $value \ "}";
  9. Echo $callback. "(" . $json.  ")";


eetopinofficial/testjsonp.php

Java code
  1. <?php
  2. Header (' Content-type:application/json ');
  3. Get Callback function name
  4. $callback = $_request [' callback '];
  5. if (Isset ($_cookie["name"])) {
  6. $name = $_cookie["name"];
  7. }else{
  8. $name = "Chopper";
  9. }
  10. JSON data
  11. $json = "{\" name\ ": \" $name \ ", \" sex\ ": \" man\ "}";
  12. Echo $json;
  13. Echo $callback;
  14. Output: callback function name (JSON data)
  15. Echo $callback. "(" . $json.  ")";
  16. echo $callback. " ($json) ";



sns/testcookie.php

Java code
    1. <script type="Text/javascript" src="Http://code.jquery.com/jquery-latest.js" ></script>
    2. <script type="Text/javascript" >
    3. $.getjson ("http://www.eetopin.com/testCookie.php?name=lunzi30108&callback=?", function (data) {
    4. alert (data.name);
    5. });
    6. </script>


sns/testjsonp.php

Java code
    1. <script type="Text/javascript" src="Http://code.jquery.com/jquery-latest.js" ></script>
    2. <script type="Text/javascript" >
    3. $.getjson ("http://www.eetopin.com/testJsonp.php?callback=?", function (data) {
    4. Alert (Data.name + "is a A" + data.sex);
    5. });
    6. </script>


Test steps:
1, http://sns.com/testCookie.php
2, http://sns.com/testJsonp.php

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.