对于我类(class)中的一个模块,我们必须打印出 json 结果,但我一生都无法让这个 json 请求在空白 HTML 文件上打印出来。是不是代码有问题?
function printResults () {
var url = "http://inserturl.com/search?app_id=123456&app_key=123456abc&format=json";
$.getJSON(url, function(jsondata){
// handle the results
document.writeln(jsondata);
});
}
如果能指出正确的方向,我们将不胜感激。
最佳答案
这段代码:
var json = $.getJSON("test.json",
function(response, status, jqXHR){
// do stuff
console.log(jqXHR.responseText);
}
);
从这个答案: Print plain JSON content to html page in Javascript / jQuery可能会帮助你。
我自己尝试了一下,效果很好。您可以粘贴以下内容并查看打印出来的数据。
var json = $.getJSON("http://ipinfo.io/162.243.142.163/json",
function(response, status, jqXHR){
document.write(jqXHR.responseText);
});
关于javascript - 在空白 HTML 文件中打印 json 请求时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23418139/