首页 > 开发 > PHP > 正文

php ajax返回 json数据实例

2024-05-04 21:59:26
字体:大 中 小
来源:转载
供稿:网友

本教程是一款php ajax返回 json数据实例,就是利用ajax实时的接受json.php文件发送的数据请求,并且进行了处理,代码如下:

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
  5. <title>php ajax返回 on数据实例</title> 
  6. <script type="text/网页特效" language="javascript"> 
  7. var xmlhttp; 
  8. function createxmlhttprequest() 
  9. { 
  10. //var xmlhttp=null; 
  11. try 
  12.   { 
  13.   // firefox, opera 8.0+, safari 
  14.   xmlhttp=new xmlhttprequest(); 
  15.   } 
  16. catch (e) 
  17.   { 
  18.   // internet explorer 
  19.   try 
  20.     { 
  21.     xmlhttp=new activexobject("msxml2.xmlhttp"); 
  22.     } 
  23.   catch (e) 
  24.     { 
  25.     xmlhttp=new activexobject("microsoft.xmlhttp"); 
  26.     } 
  27.   } 
  28. return xmlhttp; 
  29. } 
  30. function startrequest(id) 
  31. { 
  32.     createxmlhttprequest(); 
  33.     try 
  34.     {    
  35.      url="json.php?cid="+id; 
  36.         xmlhttp.onreadystatechange = handlestatechange; 
  37.         xmlhttp.open("post", url, true); 
  38.         xmlhttp.send(null); 
  39.     } 
  40.     catch(exception) 
  41.     { 
  42.         alert("xmlhttp fail"); 
  43.     } 
  44. } 
  45. function handlestatechange() 
  46. { 
  47.     if(xmlhttp.readystate == 4) 
  48.     { 
  49.         if (xmlhttp.status == 200 || xmlhttp.status == 0) 
  50.         { 
  51.             var result = xmlhttp.responsetext; 
  52.             var json = eval("(" + result + ")"); 
  53.             alert('name:'+json.name); 
  54.             alert('age:'+json.age); 
  55.    alert('id:'+json.id); 
  56.         } 
  57.     } 
  58. } 
  59. </script> 
  60. </head> 
  61.  
  62. <body> 
  63. <div> 
  64.         <input type="button" value="ajaxtest" onclick="startrequest(5);" /> 
  65.     </div> 
  66. </body> 
  67. </html> 

json.php 文件,代码如下:

  1. <?php 
  2. /************************************************************** 
  3.  * 
  4.  * 使用特定function对数组中所有元素做处理 
  5.  * @param string &$array  要处理的字符串 
  6.  * @param string $function 要执行的函数 
  7.  * @return boolean $apply_to_keys_also  是否也应用到key上 
  8.  * @access public 
  9.  * 
  10.  *************************************************************/ 
  11. function arrayrecursive(&$array, $function, $apply_to_keys_also = false) 
  12. { 
  13.     static $recursive_counter = 0; 
  14.     if (++$recursive_counter > 1000) { 
  15.         die('possible deep recursion attack'); 
  16.     } 
  17.     foreach ($array as $key => $value) { 
  18.         if (is_array($value)) { 
  19.             arrayrecursive($array[$key], $function, $apply_to_keys_also); 
  20.         } else { 
  21.             $array[$key] = $function($value); 
  22.         } 
  23.  
  24.         if ($apply_to_keys_also && is_string($key)) { 
  25.             $new_key = $function($key); 
  26.             if ($new_key != $key) { 
  27.                 $array[$new_key] = $array[$key]; 
  28.                 unset($array[$key]); 
  29.             } 
  30.         } 
  31.     } 
  32.     $recursive_counter--; 
  33. } 
  34.  
  35. /************************************************************** 
  36.  * 
  37.  * 将数组转换为json字符串(兼容中文) 
  38.  * @param array $array  要转换的数组 
  39.  * @return string  转换得到的json字符串 
  40.  * @access public 
  41.  * 
  42.  *************************************************************/ 
  43. function json($array) { 
  44.  arrayrecursive($array, 'urlencode', true); 
  45.  $json = json_encode($array); 
  46.  return urldecode($json); 
  47. }//开源代码Vevb.com 
  48.  
  49. $array = array 
  50.        ( 
  51.           'name'=>'希亚', 
  52.           'age'=>20, 
  53.     'id'=>$_post['cid'] 
  54.        ); 
  55.  
  56. echo json($array); 
  57. /********* 
  58.  {"name":"希亚","age":"20"} 
  59. ?>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表