jquery与php的json交互2

整理两个现成的函数:json_decode、json_encode
说明:其中json_encode 表示把常用的传统的数据类型如对象、数组、关联数组等转成JSON字符串。其实与JAVA里面的那个工具是一样的。而json_decode刚好相反。
解决需求1.修改
整理两个现成的函数:json_decode、json_encode
说明:其中json_encode 表示把常用的传统的数据类型如对象、数组、关联数组等转成JSON字符串。其实与JAVA里面的那个工具是一样的。而json_decode刚好相反。
解决需求1.修改数据表的时候动态生成一个JSON片段。供JS调用。
服务器端的代码:
function plan2() {
$link = mysql_connect("localhost","root","123") or die("<font color=red>无法建立起来连接。错误信息如下</font>");
mysql_query("SET NAMES gbk");
mysql_select_db("phpcms",$link) or die("<font color=red>在服务器上面无法找到此请确认已建立此DB ");
$result = mysql_query("select id,uuid,uuidtable from dytable ");
$num_rows = @mysql_num_rows($result); //看一下返回多少行记录
if ($num_rows == 0) {
$b = array(); //这样长度为0 返回的是一个空数组
}else{
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){
$b[] = $row;
}
}
echo json_encode($b);
mysql_close();
}
plan2();

这样生成的JSON是比较方便的了!

2. 客户端如果我们使用JQuery框架的话可以这样处理
<script type="text/javascript">
function ajaxcheck() {
$.ajax({
type:"GET",
url: "http://localhost/PHPCMS/projcode/?number="+Math.random(),
dataType: 'text', //注意这里面的格式形式!
success:function(msg){
$(eval(msg)).each(function(){
alert(this.id+" "+this.uuid);//得到值就可以生成多个IMG标签了!
});

}
})
}
</script>

如果客户端使用JS的话可以这样处理
<script type="text/javascript">
function ajaxcheck() {
$.ajax({
type:"GET",
url: "http://localhost/PHPCMS/projcode/?number="+Math.random(),
dataType: 'text',
success:function(msg){
json = eval(msg)
for(var i=0; i<json.length; i++)
{
alert(json[i].id+" " + json[i].uuid)
}
}
})
}
</script>



参考的一个示例代码如下:
客户端代码:
<!DOCTYPE html PUBLIC "-//W3C//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>
<script type="text/javascript" src="../scripts/jquery.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function ajaxcheck() {
$.ajax({
type:"GET",
url: "http://localhost/PHPCMS/projcode/?number="+Math.random(),
dataType: 'text',
success:function(msg){
$(eval(msg)).each(function(){
$("#output").html("<img id='"+this.id+"' src='"+this.uuid+"' />");
});
}
})
}
</script>
</head>
<body>
<button οnclick="ajaxcheck()">TEST</button>
<div id="output"></div>
</body>
</html>

服务端:
function plan2() {
$link = mysql_connect("localhost","root","123") or die("<font color=red>无法建立起来连接。错误信息如下</font>");
mysql_query("SET NAMES gbk");
mysql_select_db("phpcms",$link) or die("<font color=red>在服务器上面无法找到此请确认已建立此DB ");
$result = mysql_query("select id,uuid from dytable ");
$num_rows = @mysql_num_rows($result); //看一下返回多少行记录
if ($num_rows == 0) {
$b = array(); //这样长度为0 返回的是一个空数组
}else{
while ($row = mysql_fetch_array($result,MYSQL_ASSOC)){
$b[] = $row;
}
}
echo json_encode($b);
mysql_close();
}
plan2();

以上实现的功能是借助JSON实现的。其实FLASH也有函数解析JSON。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值