html页面ajax代码部分:
<!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" xml:lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>网页标题</title>
<meta name="keywords" content="关键字列表" />
<meta name="description" content="网页描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css"></style>
<script type="text/javascript" src="jquery-1.8.0.js"></script>
</head>
<body>
请输入数值:<input name="num" id="inp" type="text" /><input id="submit" type="submit" value="获取" />
<div id="one"><img id="imgone" src="1.png"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#submit").click(function(){
$.ajax({
type:"get",
dataType:'json',
cache:false,
data:{"num":$("#inp").val()},
url:'one.php',
success:function(data) {
$("#imgone").attr("src",data.name);
//alert("ok");
}
});
});
});
</script>
</body>
</html>
php返回json数据代码部分:
<?php
$url = $_GET['num'];
//echo $url;
$data = array();
if(is_numeric($url)){
//$data = 1;
$data['name'] =$url.".png";
$data = json_encode($data);
echo $data;
}
?>