我的 API 呼叫没有返回任何信息?

My API Call Isn't Returning Anything?

我正在检查 Twitch.com 用户是否曾经存在过。当我检查 API 调用时,它在浏览器中返回一个值,但在控制台中没有。

$.getJSON("https://api.twitch.tv/kraken/channels/comster404", function(data2){
  // console.log(data2.status);
  console.log(data2);
});

这是它应该得到的数据 { "error": "Unprocessable Entity", "status": 422, "message": "Channel 'comster404' is not available on Twitch" }

您已通过 success 回调,但没有 error 回调。由于获取 URL 时发生错误,因此不会调用 success 回调。

您可以分别使用 done()fail() 设置 successerror 的回调:

var log = document.getElementById("log");

$.getJSON("https://api.twitch.tv/kraken/channels/comster404")
  .done(function(data) {
    console.log(data);
    log.innertHTML += "success!";
  })
  .fail(function(error) {
    console.log(error);
    log.innerHTML += error.responseText;
  });
<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script>
<div id="log"></div>