如何使用javascript从url获取数据

时间:2018-05-12 07:36:49

标签: javascript php jquery json ajax

我有从其他Api控制台生成的链接 这是链接: https://SomeThing/rest1/order2/getOrders 我有从相同的休息Api控制台生成的令牌 现在我可以通过ajax ang发帖获取数据,但我想获取此数据 这是我的代码

$(document).ready(function() {
  $("button").click(function() {
    $.post(
      "https://SomeThing/rest1/order2/getOrders",
      { token: "Token" },
      function(a, b) {
       <----what i should use --->
      }
    );
  });
});

1 个答案:

答案 0 :(得分:1)

好消息是你非常接近,坏消息是你泄露了你的令牌,现在必须重新生成它。

我会用function(a,b,)替换function(data)并通过变量data

访问从被叫REST端点检索到的数据
$(document).ready(function() {
      $("button").click(function() {
        $.post(
          "https://www.tesetturpazari.com/rest1/order2/getOrders",
          { token: "<token>" },
          function(data) {
             console.log(data);
          }
        );
      });
});

然后按F12(或用于在浏览器中打开开发人员控制台的其他热键),您可以查看从远程服务器检索到的数据,并根据其结构找出如何进一步处理数据。它返回JSON format,在Javascript中很容易处理。

OP进一步解释了需要将数据呈现给站点上已存在的div(在已删除的答案的注释中),解决方案是更新回调函数:

$(document).ready(function() {
      $("button").click(function() {
        $.post(
          "https://www.tesetturpazari.com/rest1/order2/getOrders",
          { token: "<token>" },
          function(data) {
             // Possibly before displaying it all, do some preprocessing here over the data variable
             // Replace 'mydiv' with ID of an element where you want to show data
             $("#mydiv").html(data)
          }
        );
      });
});

注意:请重新生成并替换您的令牌。 注2:我重新发布了这个答案,所以我可以删除我设法复制访问令牌的原始答案,遗憾的是。