【问题标题】:Get data from baseurl with ajax使用ajax从baseurl获取数据
【发布时间】:2014-05-13 07:10:58
【问题描述】:

我想向屏幕发送信息,该信息由回显发送给它。我得到这种交互是通过ajax 发生的,想从整个屏幕接收信息再次显示。请帮忙

$(document).ready(function(){
$('#button').click(function(){
    var id = $(this).attr('id_1');

    $.ajax({ 
        url:'get.php',
        type: 'get',
        data:{id:id},
        success: function(data){
        //alert(data);
      $('#result').html(data);
        }

         });
    });
});


  <body>
    <?php if(isset($_GET['id'])){echo $_GET['id'];}?>

    <a href="#" id="button" id_1="20">test</a>
    <div id="result"></div> 

结果之后

    20
<a>test</a>****------------------------------>Is repeated
<a>test</a>  

但我只想要这个结果

     20

【问题讨论】:

  • 那么,您面临的问题是什么?
  • 你必须在每次点击函数时删除结果。这样可以避免重复 $('#result').empty()。调用这个点击开始 $('#button') .click(function(){ $('#result').empty()

标签: javascript php ajax http-get base-url


【解决方案1】:

在 AJAX 中将类型更改为 GET

$('#result').empty();
$.ajax({ 
    url:'get.php?id='+ id,
    type: "GET",
    //data:{id:id},
    success: function(data){
    //alert(data);
  $('#result').html(data);
    }

     });
});

【讨论】:

  • 感谢您的回复,但结果在标签内重复此页面
  • @user3445955 你可以检查代码。我更新以避免重复
【解决方案2】:

试试这个。它会按你说的那样显示输出。

    <script type="text/javascript">
    $(document).ready(function(){
    $('#button').click(function(){
        var id = $(this).attr('id_1');

        $.ajax({ 
            url:'get.php',
            type: "get",
            data:{id:id},
            success: function(data){
                $('#result').html(data);
            }

             });
        });
    });

    </script>

    <div id="result">
 <a  href="#" id="button" id_1="20">test</a></div> 

这是get.php

  <?php if(isset($_GET['id'])){echo 'hi';}
  ?>

如果你想要 exit:20 作为输出,那么将 get.php 更改为

 <?php if(isset($_GET['id'])){echo 'exit : '.$_GET['id'];}
    ?>

【讨论】:

  • 嘿,谢谢你的回答,但同样,正如我在运行命令时看到的那样,输出只是我想要的 hi 或 20(id_1)
  • 如果您只想输出 hi 作为输出,请按照上面编辑的方式更改 get.php
  • Tag test 重复了我不想再重复了请如果你能帮助我我只是希望我的输出 20 给你这种工作根本没有
  • 如果在 div 结果中包含代码 test 则不会再次显示。看看我的代码
  • 我设计了一个 发送到屏幕的站点,但是由于 php 是使用地址栏显示的,比如通过 ajax 如果你有 id 获取地址没有显示,但是用这段代码一切在网站上重复但通常 php 已完成这是我的代码不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多