您的位置:首页 > 其它

ajax 第一个简单程序

2013-01-13 01:20 417 查看
//////////////////////////////////////////pull.html

<html>

    <body>

        <h1> the result of pulled news:</h1>

        <script type="text/javascript" src="jquery.js"></script>

        <input type="button"  class="pull" value="abutton"/>

        <table border=1>

            <tr><td>news from server:</td></tr>

            <tr id="pullnews"></tr>

        </table>

        <script type="text/javascript">

            $(".pull").click(function(){

                     $.ajax({

                        url:"handlePull.php",

                        data:"newsid=123", //attention:paramname=value 不是paramname:value

                        type:'post',

                        dataType:'json',/////atten:如果输入json object,这里却写成text,则object解析出undefined错误

                        success:function(result){

                        //attention: 获得参数是 object.paramname ,不能被双引号引入。

                        pullnews.innerHTML="<td>"+result.title+"</td><td>"+result.digest+"</td>";

                        alert("ajax recv:"+result);

                        }});

                     });

        </script>

    </body>

</html>

////////////////////////////////////////////////////////////////////////////////handlePull.php

<?php

class news{  public $newsid,$title,$digest;}; $news=new news;

$news->newsid=$_REQUEST["newsid"];

$news->title="中日建交n年";

$news->digest="小泉纯一郎参拜靖国神社,被中国人民强烈谴责";

echo json_encode($news);

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: