首页 > 代码库 > json文件
json文件
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>$.get()方法</title>
<script src="http://www.mamicode.com/scripts/jquery-1.3.1.js" type="text/javascript"></script>
<style type="text/css">
.container{
width: 60%;
margin: 0 auto;
border:1px solid #666;
}
.comment{
background-color: #cfcfcf;
}
</style>
</head>
<body>
<div class="container">
<form action="#" id="form1">
<p>评语:</p>
<p>姓名:<input type="text" name="username" id="username"/></p>
<p>内容:<input type="text" name="content" id="content"/></p>
<p><input type="button" value="http://www.mamicode.com/提交" id="send"/></p>
</form>
<div class="comment">已有评论
<div id="resText"></div>
</div>
</div>
<script type="text/javascript">
$("#send").click(function() {
$.post(‘request.php‘, {
username:$("#username").val(),
content:$("#content").val()
},function(data,textStatus) {
// alert(data);//输出为[object Object]
var username=data.username;
var content=data.content;
var txtHtml="<div class=‘comment‘><h6>"+username+"</h6><p class=‘para‘>"+content+"</p></div>"+"\r\n";
$("#resText").append(txtHtml);
},"json");
});
</script>
</body>
</html>
//传输的文件request.php,代码如下
<?php
//json文件
header("Content-Type=text/html; utf-8:utf-8");
echo "{username:\"{$_REQUEST[‘username‘]}\",content:\"{$_REQUEST[‘content‘]}\"}"
?>
json文件