dp815292 2013-05-17 09:19
浏览 57
已采纳

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

  • 写回答

3条回答 默认 最新

  • douwen5833 2013-05-17 09:21
    关注

    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>');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵