/myFav/jason.htm

http://lefter.googlecode.com/ · HTML · 42 lines · 34 code · 8 blank · 0 comment · 0 complexity · 4eaeeb068e2f289f7afe6b817ae7ea6e MD5 · raw file

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>php json跨域</title>
  5. <script type="text/javascript">
  6. function getProfile(str) {
  7. var arr = str;
  8. document.getElementById('nick').innerHTML = arr.contact.website;
  9. }
  10. </script>
  11. </head>
  12. <body>
  13. <div id="nick"></div>
  14. <script type="text/javascript" src="http://localhost:60/singel/profile.php.php"></script>
  15. <h3>php.php代码</h3>
  16. <pre><code>
  17. < ? php
  18. $arr = array(
  19. 'name' => 'luweijun',
  20. 'nick' => 'lujo',
  21. 'contact' => array(
  22. 'email' => 'me@lefter.net',
  23. 'website' => 'http://lefter.net',
  24. )
  25. );
  26. $json_string = json_encode($arr);
  27. echo "getProfile($json_string);";
  28. //再次转换用对象的方法访问
  29. $obj = json_decode($json_string);
  30. echo ($obj->name)
  31. ?>
  32. </code></pre>
  33. </body>
  34. </html>