读书人

jquery基础解决方法

发布时间: 2012-08-31 12:55:03 作者: rapoo

jquery基础

HTML code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html>    <head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    <title>jquery与ajax的应用</title>    <style type="text/css">        * { margin:0; padding:0;}        body { font-size:12px;}        .comment { margin-top:10px; padding:10px; border:1px solid #ccc;background:#DDD;}        .comment h6 { font-weight:700; font-size:14px;}        .para { margin-top:5px; text-indent:2em;background:#DDD;}    </style>    <script type="text/javascript" src="scripts/jquery-1.7.2.js" ></script>    <script type="text/javascript">        $(function() {                           $("#send").click(function() {                $.post("get3.php",{ //向get3.php页面发送post请求                    username:$("#username").val(),//发送数据                    content:$("#content").val()                        },function(data,textStatus) {  //回调函数                    var username = data.username;                    var content = data.content;                    var htmltext = "<div class='comment'><h6>"+username+"</h6><p class='para'>"+content+"</p></div>";                    $("#resText").html(htmltext);//将返回的json数据以html的形式插入到页面中                },"json");            });                    });        </script>    </head>        <body>        <!---------------------- post()方法--------------------->        <form id="form1" action="#">        <p>评论:</p>         <p>姓名: <input type="text" name="username" id="username" /></p>         <p>内容: <textarea name="content" id="content"  rows="2" cols="20"></textarea></p>         <p><input type="button" id="send" value="提交"/></p>        </form>        <div  class='comment'>已有评论:</div>        <div id="resText" ></div>                </body></html>


PHP code
    header("Content-Type: text/html;charset=utf-8");    echo "{ username : \"{$_REQUEST['username']}\" , content : \"{$_REQUEST['content']}\"}" 


[解决办法]
JScript code
$(function() {                $("#send").click(function() {                $.post("get3.php",{ //向get3.php页面发送post请求                    username:$("#username").val(),//发送数据                    content:$("#content").val()                        },function(result) {  //回调函数                    $("#resText").html(result);//将返回的json数据以html的形式插入到页面中                },"html");            });                    });
[解决办法]
echo "{ "username" : \"{$_REQUEST['username']}\" , "content" : \"{$_REQUEST['content']}\"}"

试试看~
[解决办法]
如果是1.4后的版本,json的格式求要很严格的。要像这样写了。
探讨
echo "{ "username" : \"{$_REQUEST['username']}\" , "content" : \"{$_REQUEST['content']}\"}"



试试看~

读书人网 >JavaScript

热点推荐