Home > php教程 > php手册 > php ajax返回 json数据实例

php ajax返回 json数据实例

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 09:14:11
Original
1376 people have browsed it

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

nbsp;html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
	 
	 
	<meta> 
	<title>php ajax返回 on数据实例</title> 
	<script> 
	var xmlhttp; 
	function createxmlhttprequest() 
	{ 
	//var xmlhttp=null; 
	try 
	  { 
	  // firefox, opera 8.0+, safari 
	  xmlhttp=new xmlhttprequest(); 
	  } 
	catch (e) 
	  { 
	  // internet explorer 
	  try 
	    { 
	    xmlhttp=new activexobject("msxml2.xmlhttp"); 
	    } 
	  catch (e) 
	    { 
	    xmlhttp=new activexobject("microsoft.xmlhttp"); 
	    } 
	  } 
	return xmlhttp; 
	} 
	function startrequest(id) 
	{ 
	    createxmlhttprequest(); 
	    try 
	    {    
	     url="json.php?cid="+id; 
	        xmlhttp.onreadystatechange = handlestatechange; 
	        xmlhttp.open("post", url, true); 
	        xmlhttp.send(null); 
	    } 
	    catch(exception) 
	    { 
	        alert("xmlhttp fail"); 
	    } 
	} 
	function handlestatechange() 
	{ 
	    if(xmlhttp.readystate == 4) 
	    { 
	        if (xmlhttp.status == 200 || xmlhttp.status == 0) 
	        { 
	            var result = xmlhttp.responsetext; 
	            var json = eval("(" + result + ")"); 
	            alert(&#39;name:&#39;+json.name); 
	            alert(&#39;age:&#39;+json.age); 
	   alert(&#39;id:&#39;+json.id); 
	        } 
	    } 
	} 
	</script> 
	 
	 
	 
	<div> 
	        <input> 
	    </div> 
	 
	 
Copy after login

json.php 文件,代码如下:

<?php 
	/************************************************************** 
	 * 
	 * 使用特定function对数组中所有元素做处理 
	 * @param string &$array  要处理的字符串 
	 * @param string $function 要执行的函数 
	 * @return boolean $apply_to_keys_also  是否也应用到key上 
	 * @access public 
	 * 
	 *************************************************************/ 
	function arrayrecursive(&$array, $function, $apply_to_keys_also = false) 
	{ 
	    static $recursive_counter = 0; 
	    if (++$recursive_counter > 1000) { 
	        die(&#39;possible deep recursion attack&#39;); 
	    } 
	    foreach ($array as $key => $value) { 
	        if (is_array($value)) { 
	            arrayrecursive($array[$key], $function, $apply_to_keys_also); 
	        } else { 
	            $array[$key] = $function($value); 
	        } 
	 
	        if ($apply_to_keys_also && is_string($key)) { 
	            $new_key = $function($key); 
	            if ($new_key != $key) { 
	                $array[$new_key] = $array[$key]; 
	                unset($array[$key]); 
	            } 
	        } 
	    } 
	    $recursive_counter--; 
	} 
	 
	/************************************************************** 
	 * 
	 * 将数组转换为json字符串(兼容中文) 
	 * @param array $array  要转换的数组 
	 * @return string  转换得到的json字符串 
	 * @access public 
	 * 
	 *************************************************************/ 
	function json($array) { 
	 arrayrecursive($array, &#39;urlencode&#39;, true); 
	 $json = json_encode($array); 
	 return urldecode($json); 
	}//开源代码phprm.com 
	 
	$array = array 
	       ( 
	          &#39;name&#39;=>&#39;希亚&#39;, 
	          &#39;age&#39;=>20, 
	    &#39;id&#39;=>$_post[&#39;cid&#39;] 
	       ); 
	 
	echo json($array); 
	/********* 
	 {"name":"希亚","age":"20"} 
	
Copy after login

文章地址:

转载随意^^请带上本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template