版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章原始出版、作者信息和本声明。否则将追究法律责任。http://blog.csdn.net/mayongzhan - 马永占,myz,mayongzhan
ajax使用json解决复杂数据传递,xml很好用,不过似乎xml文件要比json格式的大.而且js操作不方便.
其实使用自定义的格式也很好,不过通用性不强,为什么不使用一个标准的东西呢?json就很好.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
json_decode ( string $json [, bool $assoc ] )
json_encode ( mixed $value )
JSON.parse(text, filter)
JSON.stringify(value, whitelist)
原理很简单.就是把一个js数组编成json,然后xmlrpc传给proc.php,里面解开json,然后处理,然后再编回json,返回给test.php
test.php那到返回值,然后解成js数组使用.
json2.js去json网站上下
xmlrpc的去网上搜一下
------------------------------------------------------------------------------------------
test.php
<!DOCTYPE html PUBLIC "-//W<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><chmetcnv w:st="on" tcsc="0" numbertype="1" negative="False" hasspace="False" sourcevalue="3" unitname="C">3C</chmetcnv>//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="马永占(MyZ)" />
<meta name="Copyright" content="马永占(MyZ)" />
<meta name="description" content="" />
<meta name="keywords"content="" />
<link rel="icon" href="" type="image/x-icon" />
<link rel="shortcut icon" href="" type="image/x-icon" />
<link href="" rel="stylesheet" type="text/css" />
<title></title>
<script type="text/javascript" src="xmlhttp.js"></script>
<script type="text/javascript" src="json2.js"></script>
</head>
<body>
<input type="button" name="button" value="click me" οnclick="getMain();" />
<textarea name="main" id="main" cols="20" rows="10"></textarea>
<textarea name="main2" id="main2" cols="20" rows="10"></textarea>
<script type="text/javascript">
function getMain(){
var a = Math.floor(9*Math.random());
var b = Math.floor(9*Math.random()) + 10;
var c = Math.floor(9*Math.random()) + 20;
var d = Math.floor(9*Math.random()) + 30;
var post = {
"b":b,
"c":c,
"d":d
}
var json_string = JSON.stringify(post);
var xmlhttp = new XmlHttp();
var sUrl = "proc.php?a="+a;
var method = "POST"; //var method = "GET";
var post_params = "json_string=" + json_string;
var mimeType = "application/x-www-form-urlencoded";
xmlhttp.response(sUrl,post_params,"getMainHttpCallback",method,mimeType);
}
function getMainHttpCallback(obj){
//var arr = eval('(' + obj.responseText + ')');
var arr = JSON.parse(text, function (key, value) {
return key.indexOf('date') >= 0 ? new Date(value) : value;
});
document.getElementById('main').value = "a: " + arr["get"] + "\r\n" + "b: " + arr["post"]["b"];
document.getElementById('main2').value = obj.responseText;
}
</script>
</body>
</html>
------------------------------------------------------------------------------------------
proc.php
<?php
/**
* @name proc.php
* @date Tue Sep 16 23:49:23 CST 2008
* @copyright 马永占(MyZ)
* @author 马永占(MyZ)
* @link http://blog.csdn.net/mayongzhan/
*/
$post = isset($_POST['json_string']) ? $_POST['json_string'] : 'no post';
$get = isset($_GET['a']) ? $_GET['a'] : 'no get';
$post = json_decode($post,true);
foreach ($post as &$value) {
$value = 'x' . $value;
}
$get = 'a' . $get;
$arr = array('post'=>$post,'get'=>$get);
echo json_encode($arr);
?>