js获取请求信息

qq_20250763 2020-04-23 09:37:50

<script>
function req() {
$.ajax({
type:"GET",
url: '../Bt.php',
dataType: 'json',
success: function(res) {

console.log(res['cpuRealUsed']);//备注

},
error: function() {
console.log('请求失败~');
}
});
}
req();
setInterval(req, 1000);


function echar(){
//我想在这里获取上面 res['cpuRealUsed'] 信息
}

</script>





请各位大神帮忙指点一下!
...全文
320 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
浴火_凤凰 2020-06-30
  • 打赏
  • 举报
回复

<script>
function echar(data){
	console.log(data);
}
 function req() {
  $.ajax({
      type:"GET",
      url: '../Bt.php',
      dataType: 'json',
      success: function(res) {

		console.log(res['cpuRealUsed']);//备注
 echar(res['cpuRealUsed']);
        
      },
      error: function() {
          console.log('请求失败~');
      }
  });
}
req();
setInterval(req, 1000); 




</script>
Adam_World 2020-04-29
  • 打赏
  • 举报
回复
<script>
var res = null;
function req() {
$.ajax({
type:"GET",
url: '../Bt.php',
dataType: 'json',
success: function(res) {
res = res['cpuRealUsed'];
console.log(res['cpuRealUsed']);//备注

},
error: function() {
console.log('请求失败~');
}
});
}
req();
setInterval(req, 1000);


function echar(){
//我想在这里获取上面 res['cpuRealUsed'] 信息
console.log(res);
}

</script>


用var先定义一个null空对象,由于var定义的变量具有变量提升的作用,所以在内部直接给参数赋值,该参数就成为了全局变量,可以在任何地方访问或者调用了
jio可 2020-04-24
  • 打赏
  • 举报
回复

<script>
  var timr = null
  function echar(res) {
    console.log(res['cpuRealUsed']); //备注
  }

  function req() {
    $.ajax({
      type: "GET",
      url: '../Bt.php',
      dataType: 'json',
      success: function (res) {
        echar(res)
        init()
      },
      error: function () {
        console.log('请求失败~');
      }
    });
  }
  req();
  function init(){
    timr && clearTimeout(timr)
    timr = setTimeout(req, 1000)
  }
</script>
不要用setInterval,如果响应超时,重连成功会导致一下子接受之前堆积的所有请求结果
qq_20250763 2020-04-24
  • 打赏
  • 举报
回复
引用 2 楼 啊柒_ 的回复:

    function echar(res){
      console.log(res['cpuRealUsed']);//备注
    }
    function req() {
      $.ajax({
          type:"GET",
          url: '../Bt.php',
          dataType: 'json',
          success: function(res) {
            echar()
             
          },
          error: function() {
              console.log('请求失败~');
          }
      });
    }
    req();
    setInterval(req, 1000); 
这样不行?
这样不行,调不出来呢
usecf 2020-04-24
  • 打赏
  • 举报
回复
function req() { $.ajax({ type: "GET", url: '../Bt.php', dataType: 'json', success: function (res) { echar(res) //把res传递出去,在echar方法解析你需要的字段 init() }, error: function () { console.log('请求失败~'); } }); }
啊柒_ 2020-04-23
  • 打赏
  • 举报
回复

    function echar(res){
      console.log(res['cpuRealUsed']);//备注
    }
    function req() {
      $.ajax({
          type:"GET",
          url: '../Bt.php',
          dataType: 'json',
          success: function(res) {
            echar()
             
          },
          error: function() {
              console.log('请求失败~');
          }
      });
    }
    req();
    setInterval(req, 1000); 
这样不行?

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧