美文网首页
PHP编程实战15-14/15

PHP编程实战15-14/15

作者: 海边拾贝 | 来源:发表于2015-11-09 21:43 被阅读0次

    前端

    <!--PHP编程实战-->
    <!--JSON & Ajax -->
    <!--15-15-->
    <!--使用$.getJSON和.each-->
    <html>
    <head>
        <title>Loading JSON with jQuery</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                    $.getJSON("json_example.php", function (data) {
                        $.each(data, function (continent, animals) {
                            var message = "<strong>" + continent + "</strong></br>";
                            for (j = 0; j < animals.length; ++j) {
                                message += animals[j] + ", ";
                            }
                            // remove last comma  and space
                            message = message.trim();
                            message = message.substring(0, message.length - 1);
                            $("#generated_content").append("<p>" + message + "</p>");
                        })
                    })
                }
            )
        </script>
    </head>
    <body>
    <p><strong>Ajax parsed XML:</strong></p>
    
    <div id="generated_content"> </div>
    </body>
    </html>
    

    服务器端json_example.php返回json字符串

    <?php
    $animals = array(
        "africa" => array("gorilla", "giraffe", "elephant"),
        "asia" => array("panda"),
        "north america" => array("grizzly bear", "lynx", "orca"),
    );
    print json_encode($animals);
    ?>
    

    重点

    • 请求资源文件在PHP编程实战15-14
    • $.each方法规定为每个匹配元素规定运行的函数。
      提示:返回 false 可用于及早停止循环。
      语法:$(selector).each(function(index,element))
    • $.getJSON这里虽然请求的是json_example.php这样的php文件,但返回结果是json字符串.php只是一个标记生成器.
      $.getJSON
      $.each

    问题
    js不同函数回调函数的几种形式是在哪里定义好的?

    相关文章

      网友评论

          本文标题:PHP编程实战15-14/15

          本文链接:https://www.haomeiwen.com/subject/hwchhttx.html