如何正确使用jquery的getJSON?

时间:2022-02-05 16:48:10
<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $("#hidebtn").click(function() {
                    $("#text").slideToggle("slow", function() {
                        $("#hidebtn").html($(this).is(":visible") ? "cacher" : "montrer");
                    });
                });
                $.getJSON("http://localhost/test/nice.php", {name: "Sam", age: 17}, function(data) {
                    alert("alert");
                });
            });
        </script>
    </head>
    <body style="width: 500px; margin: auto; text-align: center;">
        <p style="border: 1px solid black;" id="text">
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sem urna, ornare ac consectetur sit amet, pharetra eget metus. Duis eu arcu dolor, quis accumsan justo. Donec eget molestie sem. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nam ut purus at diam semper consequat vel vitae velit. Maecenas risus augue, mollis ut dapibus non, dictum eget dolor. Cras eu velit nec diam tempor aliquet eu ut nulla. Donec gravida, felis eget congue ultrices, tellus elit iaculis massa, rhoncus faucibus lorem dui in tellus. Curabitur ut dignissim nunc. Curabitur feugiat posuere metus, placerat porttitor tortor feugiat ac. Vestibulum ut leo est, a hendrerit magna. Aenean feugiat ante id leo ultricies et dictum nibh ultricies. Curabitur eleifend venenatis imperdiet. Donec a scelerisque enim. Fusce at lacus odio, in varius orci.

            Etiam varius suscipit lectus id condimentum. Donec in ligula ac turpis porta tempor ac vel erat. Aliquam accumsan, nulla in egestas gravida, leo massa ultricies risus, at tempus justo orci sit amet est. Duis eu massa magna. Sed in gravida ipsum. Mauris lobortis, nulla vel ullamcorper fermentum, massa tellus feugiat neque, nec semper sapien eros ut dui. Cras dignissim, orci sit amet vehicula luctus, nisi urna venenatis est, ut bibendum urna augue nec dolor. Suspendisse sed ipsum nunc. Morbi eros tellus, volutpat ut ullamcorper nec, semper id ipsum. Aliquam quis sem ligula, in cursus enim. Curabitur laoreet, magna et interdum gravida, mauris metus lobortis orci, sed sollicitudin est magna a tortor. Phasellus eget porta nisl. Aliquam at quam urna. Curabitur in commodo eros. Aenean laoreet leo ut dolor rhoncus interdum. Praesent et mi leo, quis tincidunt nunc. Mauris sagittis elementum purus, ut feugiat tortor cursus a. Nam ut dolor dui. Suspendisse vulputate luctus tortor, consequat pharetra ante laoreet a. Nullam placerat urna quis turpis vestibulum tempor.
        </p>
        <div style="background-color: green; width: 100px; margin: auto;" id="hidebtn">cacher</div>
    </body>
</html>

nice.php source file

nice.php源文件

<?php

header('Content-Type: application/json');
echo json_encode(array('name' => 'Sam', 'age' => 17));

?>

It don't get any alert, as if there was no data sent. What did I miss ?

它没有得到任何警报,好像没有发送数据。我错过了什么 ?

1 个解决方案

#1


3  

what tool are you using to debug your javascript? try using firebug to see what JSON tab is in console this will give you a list of objects and what is being sent over to the php script

你用什么工具来调试你的javascript?尝试使用firebug来查看控制台中的JSON选项卡,这将为您提供一个对象列表以及发送到php脚本的内容

#1


3  

what tool are you using to debug your javascript? try using firebug to see what JSON tab is in console this will give you a list of objects and what is being sent over to the php script

你用什么工具来调试你的javascript?尝试使用firebug来查看控制台中的JSON选项卡,这将为您提供一个对象列表以及发送到php脚本的内容