简体   繁体   English

我的API调用什么都没有返回?

[英]My API Call Isn't Returning Anything?

I'm checking to see if a Twitch.com user has ever existed. 我正在检查Twitch.com用户是否曾经存在。 When I check the API call it is returning a value in the browser, but not in the console. 当我检查API调用时,它在浏览器中返回一个值,但在控制台中未返回。

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

This is the data it should get { "error": "Unprocessable Entity", "status": 422, "message": "Channel 'comster404' is not available on Twitch" } 这是应该获取的数据{“” error“:” Unprocessable Entity“,” status“:422,” message“:” Twitch上的频道'comster404'不可用“}

You've passed a success callback but no error callback. 您已经传递了success回调,但没有error回调。 Since an error occurred when fetching that URL, the success callback would not be called. 由于获取该URL时发生错误,因此不会调用success回调。

You can set the callbacks for success and error with done() and fail() respectively: 您可以分别使用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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM