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
- <?php
- Header (' Content-type:application/json ');
- //Get callback function name
- $callback = $_get [' callback '];
- $value = $_get[' name '];
- //setcookie ("name", "", Time ()-3600);
- Setcookie (' name ', $value, Time () +3600*), ' /','. eetopin.com ', $_server[' server_port ']== 443? 1 : 0 );
- $json = "{\" name\ ": \" Add cookie success $callback $value \ "}";
- Echo $callback. "(" . $json. ")";
eetopinofficial/testjsonp.php
Java code
- <?php
- Header (' Content-type:application/json ');
- //Get callback function name
- $callback = $_request [' callback '];
- if (Isset ($_cookie["name"])) {
- $name = $_cookie["name"];
- }Else{
- $name = "Chopper";
- }
- //json Data
- $json = "{\" name\ ": \" $name \ ", \" sex\ ": \" man\ "}";
- //echo $json;
- //echo $callback;
- //Output: callback function name (JSON data)
- Echo $callback. "(" . $json. ")";
- //echo $callback. " ($json) ";
sns/testcookie.php
Java code
- <script type= "Text/javascript" src= "Http://code.jquery.com/jquery-latest.js" ></script>
- <script type="Text/javascript">
- $.getjson ("http://www.eetopin.com/testCookie.php?name=lunzi30108&callback=?" ) , function (data) {
- alert (data.name);
- });
- </script>
sns/testjsonp.php
Java code
- <script type= "Text/javascript" src= "Http://code.jquery.com/jquery-latest.js" ></script>
- <script type="Text/javascript">
- $.getjson ("http://www.eetopin.com/testJsonp.php?callback=?" ) , function (data) {
- Alert (Data.name + "is a A" + data.sex);
- });
- </script>
Test steps:
1, http://sns.com/testCookie.php
2, http://sns.com/testJsonp.php