weixin_33724046 2017-01-02 11:28 采纳率: 0%
浏览 359

$ .getJSON调用不起作用

I am beginner to code and right now working on a challenge in FCC. The problem is I am unable to run a $.getJSON call in codepen. I have read previous question on Stack Overflow but still was not able to comprehend. So please somebody shed a light to clear my code-learning roadblock.

$(document).ready(function(){
var url = "https://api.twitch.tv/kraken/streams/freecodecamp?callback=?";
 $.getJSON(url,function(data1){
   if(data1.stream===null){
     $("#fccStatus").html("FCC is currently OFFLINE");
   }
   else{
     $("#fccStatus").html("FCC is ONLINE");
   }
 });
});

I am even not able to run this code in codepen

$(document).ready(function(){
  $.getJSON('data.json',function(result){
    alert("success");
  });
});

I am using Google Chrome, but also have tried this in Firefox

The HTML code is as follows:

 <div class="container">
      <div id="header" class="text-center">
  <h1>TwitchTV JSON API</h1>
  <h2>Free Code Camp</h2>
  <h3 id="fccStatus"></h3>
  </div>
  <div class="row">
    <h1>
    <div class="col-md-4">
      Logo:
      </div>
     <div class="col-md-4">
      Display Name:
      </div>
      <div class="col-md-4">
      Status:
      </div>
    </h1>
  </div>
  <div id="followerInfo">

  </div>
</div>
  • 写回答

1条回答 默认 最新

  • weixin_33690963 2017-01-02 11:46
    关注

    EDIT: You included jquery-ui and NOT jquery in your codepen, hence it doesn't work because of that! Twitch changed its API and you need to register a clientID if you want to make any requests to their API. You can go to this webpage and follow the instructions there. What is happening to your requests right now is a 400 error each time you make a request. Quoting the API documentation: To identify your application to the API, every request must include your application’s client ID, either explicitly or implicitly by including an OAuth token. If you use an OAuth token in your request, the API figures out the client ID for you.

    Requests without a client ID fail with an HTTP 400 error.

    To get a client ID, register a developer application on the connections page of your Twitch account. Once you have your client ID, you can send it via:

    Request header (Client-ID: XXXXX) Query-string parameter (https://api.twitch.tv/kraken/users/44322889?client_id=XXXXX)

    Old post, for sake of full disclosure:

    You should upload your HTML to make sure that you actually included the files correctly.

    I tried this code, which is like yours, in codepen and everything works fine

    $(document).ready(function(){
      $.getJSON('https://jsonplaceholder.typicode.com/posts/1',function(result){
        console.log("success");
      });
    });
    

    I think you might have a problem with a CDN where jquery is at, but before that, given that you have a reference error and jQuery is not defined. You can look at this pen I wrote for you: Codepen example

    If you upload your HTML, like you did in the comments under your post, you will notice that this:

     <div class="container">
          <div id="header" class="text-center">
      <h1>TwitchTV JSON API</h1>
      <h2>Free Code Camp</h2>
      <h3 id="fccStatus"></h3>
      </div>
      <div class="row">
        <h1>
        <div class="col-md-4">
          Logo:
          </div>
         <div class="col-md-4">
          Display Name:
          </div>
          <div class="col-md-4">
          Status:
          </div>
        </h1>
      </div>
      <div id="followerInfo">
    
      </div>
    </div>
    

    Is not friendly formatted, which is fine for now, but also doesn't include any mentions to any javascript or jquery files. Where are the DOCTYPE, html, body, head tags? You are missing all the script tags that connect your script and enable jQuery to run.

    <html>
    <head></head>
     <body>
     <div class="container">
          <div id="header" class="text-center">
      <h1>TwitchTV JSON API</h1>
      <h2>Free Code Camp</h2>
      <h3 id="fccStatus"></h3>
      </div>
      <div class="row">
        <h1>
        <div class="col-md-4">
          Logo:
          </div>
         <div class="col-md-4">
          Display Name:
          </div>
          <div class="col-md-4">
          Status:
          </div>
        </h1>
      </div>
      <div id="followerInfo">
    
      </div>
    </div>
    <script src="/path/to/jquery.js"></script>
    <script src="/path/to/your-script.js"></script>
    </body>
    </html>
    

    Also, since you are doing the FCC API Challenge, you might want to refer to this tutorial post, just if you get stuck tho, otherwise it doesn't make sense. FCC Twitch API

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端