dp815292
dp815292
2013-05-17 09:19

Jquery $ .get()从带有JSON的php脚本返回未定义的结果

  • json
  • php
  • ajax
  • jquery

updated

Err i try to see more tutorial and i decided to use $.get() first since its easier and good for starting point..

so this is the script and i think it works correctly except it gives undefined result

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Display Json</title>
<script src="../_js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $(document).ready(function()
    {
        $('#jsonButton').click(function()
        {
            var data = ''
            $.get('getJson.php', data, response);
        });//end click
    });//end ready

    function response(data)
    {
        $('#display').prepend('<p>' + data.name + data.phone + '</p>');
    }
</script>
<body>
    <div id="display">

            <input type="button" id="jsonButton" value="getJson!" />

    </div>
</body>
</html>

and this is the getJson.php simple php script that returns simple JSON object :

$data['name'] = 'username';
$data['phone'] = '08989808089';

header('Content-Type: application/json');
echo json_encode($data);

when i click the 'getJson' button, it displays undefined

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答