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

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

that is because your selector is incorrect

$('submit').click(function()
//-^^^^^----here

it should be

 $('input[name="submitButton"]').click(function(){
  ....  
}

OR give an id to your button and use id seletor with #

 <input type="submit" name="submitButton" value="getJson!" id="getjson"/>

  $('#getjson').click(function(){
   ......

OR you can use

$('input:submit').click(function(){ 
  .....
});

updated

and for undefined you can call the callback function .....

$.get('getJson.php', data, function(data){
    $('#display').prepend('<p>' + data.name + data.phone + '</p>');
});

You need to select the correct button to bind click event.

$('input:submit').click(function(){  });

I'm not 100% sure you've got the selector right. I'd give the button an id and use that.

<script type="text/javascript">
    $(document).ready(function()
    {
        $('#mybutton').click(function()
        {
            $.ajax(
            {
                type: 'GET',
                url: 'getJson.php',
                dataType: 'json',
                success: function(jsonData)
                {
                    $('#display').prepend('<p>' + jsonData.name + '  ' + jsonData.phone + '</p>');
                }
            });//end ajax
            return false;
        });//end click
    });//end ready

</script>
    <body>
        <div id="display">

                <input id="mybutton" type="button" name="submitButton" value="getJson!" />

        </div>
    </body>

Read about proper jquery selectors here

http://www.w3schools.com/jquery/jquery_ref_selectors.asp

There are many different selectors you could use to attach the click event to that button.

Edit: Change the type of the input to button as well... submit is for a form