weixin_33749131 2017-09-20 16:11 采纳率: 0%
浏览 5

如何使用此API

I try to consume this api (https://ministerio.herokuapp.com) with jQuery , when I run the script the result is a blank html page

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript - read JSON from URL</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
  </head>

  <body>
    <div class="mypanel"></div>

    <script>
      $.getJSON('https://ministerio.herokuapp.com', function(data) {     
        var text = `Indexers: ${data.Indexers}`
        $(".mypanel").html(data);
      });
    </script>
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • 普通网友 2017-09-21 23:52
    关注

    You first had a No 'Access-Control-Allow-Origin' header is present on the requested resource. error when requesting the json ressource.

    Congratulations, you fixed the header of the ressource.
    It wasn't clear you had control over it at first.

    Now, that resource isn't outputing a json yet.
    The next error you will have to manage is parsererror.

    How did I found that error?

    I used the .fail() callback.
    See in CodePen

    $.getJSON('https://ministerio.herokuapp.com', function(data) {     
      // Whatever you do here in case of success.
    
    }).fail(function(request,error){
      console.log(request);
      console.log(error);
    });
    

    You expect the result to be:

    {"Ambiental":"1","Descripcion":"Mayormente nublado","Fecha_Actualizacion":"2017-09-21T18:09:51-05:00","Humedad":"70%","Picoyplaca":"1 - 3 - 5 - 7 - 9","Publico":"1 - 2","TasaRM":" $ 2,913.96","Temperatura":"14°"}
    

    But, here is what your ressource outputs at the moment:

    <p> {&quot;Ambiental&quot;:&quot;1&quot;,&quot;Descripcion&quot;:&quot;Mayormente nublado&quot;,&quot;Fecha_Actualizacion&quot;:&quot;2017-09-21T18:09:51-05:00&quot;,&quot;Humedad&quot;:&quot;70%&quot;,&quot;Picoyplaca&quot;:&quot;1 - 3 - 5 - 7 - 9&quot;,&quot;Publico&quot;:&quot;1 - 2&quot;,&quot;TasaRM&quot;:&quot; $ 2,913.96&quot;,&quot;Temperatura&quot;:&quot;14°&quot;}
    


    So... Get rid of that unclosed <p>tag first.

       You should post that ressource code... As many weird things may be found there.

    But I'm quite sure the next step will be to just replace those HTML entities by the real characters needed.

    Then use json_encode() to echo the result to make sure it is properly formated if the data comes from an array (I suppose that is a PHP script).

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog