jquery ajax(3).post
.post返回文本数据
$(function(){ $("#send").click(function(){ $.post("post1.php", { username : $("#username").val() , content : $("#content").val() }, function (data, textStatus){ $("#resText").html(data); // 把返回的数据添加到页面上 } ); }) })
php文件
<?php header("Content-Type:text/html; charset=utf-8"); echo "<div class='comment'><h6>{$_REQUEST['username']}:</h6><p class='para'>{$_REQUEST['content']}</p></div>"; ?>
.post 返回xml数据
$(function(){ $("#send").click(function(){ $.post("post2.php", { username : $("#username").val() , content : $("#content").val() }, function (data, textStatus){ var username = $(data).find("comment").attr("username"); var content = $(data).find("comment content").text(); var txtHtml = "<div class='comment'><h6>"+username+":</h6><p class='para'>"+content+"</p></div>"; $("#resText").html(txtHtml); // 把返回的数据添加到页面上 },"xml"); }) })
php 文件
<?php header("Content-Type:text/xml; charset=utf-8"); echo "<?xml version='1.0' encoding='utf-8'?>". "<comments>". "<comment username='{$_REQUEST['username'] }' >". "<content>{$_REQUEST['content']}</content>". "</comment>". "</comments>"; ?>
.post 返回json数据实现 $(function(){ $("#send").click(function(){ $.post("post3.php", { username : $("#username").val() , content : $("#content").val() }, function (data, textStatus){ var username = data.username; var content = data.content; var txtHtml = "<div class='comment'><h6>"+username+":</h6><p class='para'>"+content+"</p></div>"; $("#resText").html(txtHtml); // 把返回的数据添加到页面上 },"json"); }) })
php文件
<?php header("Content-Type:text/html; charset=utf-8"); echo "{ \"username\" : \"{$_REQUEST['username']}\" , \"content\" : \"{$_REQUEST['content']}\"}" ?>
.load注意是
$(function(){ $("#send").click(function(){ $("#resText").load("post1.php",{ username : $("#username").val() , content : $("#content").val() }) }) })
<?php header("Content-Type:text/html; charset=utf-8"); echo "<div class='comment'><h6>{$_REQUEST['username']}:</h6><p class='para'>{$_REQUEST['content']}</p></div>"; ?>
没有无缘无故的荣耀
赞助我写出更好的博客

赞助我写出更好的博客