doukunsan5553 2015-09-06 01:12
浏览 38

使用AJAX计时器获取“PHP URL内容”

I have many <a href> on my page to generate link like
http://localhost/ozania/order.php?cat_id=DS01

"DS01" is ony an example..

So I want to get values that every link I pressed (in the same page)

function getCAT(){
   $.ajax({
      type: "GET",
    url: "order.php",
    data: {
        cat_id: 'cat_id'
    },
      success: function(response){
       $("#category").text(""+response+"");
       timer = setTimeout("getCAT()",5000);
      }
     });  
  }
  $(document).ready(function(){
   getCAT();
  });

and print the result in <span> like

<span id="category"></span>

but it always printed as nothing....

every i pressed the link, it is not reloading page because i use that script like

<a onclick="window.history.pushState('', '', '?cat_id=<?php echo $row['category_item']; ?>');"><?php echo $row['name_item'];?> </a>

So finally I want to print the link values every link I pressed with AJAX and preventing pages to reload.

  • 写回答

1条回答 默认 最新

  • dongqiang8683 2015-09-06 02:28
    关注

    I'm not 100% sure I understand what you're asking, but I think you're asking how to add a div with and id "category" somewhere on the page that contains data for each link?

    First, html ids should be unique, use a class instead.

    Second, if the span does not already exist, you cannot change its text.

    Maybe you want to do something more like

    $('#somedivthatexists').append('<span class="category">'+response+'</span>');
    

    Also you cannot call a function like this.

    Instead try something like

    getCat = function(){// the same code in here}
    

    Otherwise you wont be able to call it recursively the way you are trying.

    Finally, I don't see an end to the recursion. Maybe you want do loop through all links and call the get cat function on them instead of doing some strange recursion here?

    Best of luck.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题