有人可以指出为什么这个回复没有打印到页面上这显而易见吗?
$.ajaxPrefilter( function (options) {
if (options.crossDomain && jQuery.support.cors) {
var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
contentType: 'application/json; charset=utf-8',
options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
//options.url = "http://cors.corsproxy.io/url=" + options.url;
}
});
$.get(
'https://data.nba.com/data/v2015/json/mobile_teams/nba/2017/scores/gamedetail/0011700003_gamedetail.json',
function (data) {
console.log("THE RESPONSE ", data);
setTimeout(function(){
$("#viewer").html(data);
}, 4000);
});
HTML
<div id="viewer"></div>
https://jsfiddle.net/nolaandy/df0jsdL0/1/
这里的JSFiddle显示在控制台中记录了一个JSON对象。 我想访问响应中的数据。我以为我只是打印出对“观众”div的整个回复,但没有出现在那里。
答案 0 :(得分:1)
没有呈现任何内容的原因是因为$.html()
接受要呈现为HTML的字符串,并且您提供了一个对象。
如果您希望将对象打印为字符串,则可以重构为:
$("#viewer").html(JSON.stringify(data));
我认为这会非常嘈杂,特别是如果物体很大的话。出于调试目的,我认为您最好使用控制台。