Javascript 从本地计算机运行程序时,如何检索此URL中的数据?
我正在测试一个网站,并试图通过我的本地机器在我网站的URL内运行数据,但它不起作用Javascript 从本地计算机运行程序时,如何检索此URL中的数据?,javascript,json,ajax,local-storage,Javascript,Json,Ajax,Local Storage,我正在测试一个网站,并试图通过我的本地机器在我网站的URL内运行数据,但它不起作用 function getQuotes() { return $.ajax({ headers: { Accept: "application/json" }, url: 'https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e
function getQuotes() {
return $.ajax({
headers: {
Accept: "application/json"
},
url: 'https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json',
success: function(jsonQuotes) {
if (typeof jsonQuotes === 'string') {
quotesData = JSON.parse(jsonQuotes);
console.log('quotesData');
console.log(quotesData);
}
}
});
}
当我从IDE运行站点时,引号框中应该填充JSON数据。根据您使用的jQuery版本,您只需使用
$.getJSON()
以下是一个例子:
(函数(){
变量url=”https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json";
$.getJSON(url)
.完成(功能(数据){
//确保data.quotes是一个数组
if(Array.isArray(data.quotes)){
//在报价框中追加每个报价
$.each(data.quotes,函数(i,项){
var quoteText=item.quote+”(“+item.author+”);
$(“#quotesBox”).append(“+quoteText+”);
});
}
});
})();代码>
blockquote{
字体系列:佐治亚州,“新罗马时代”,衬线;
字体:斜体;
背景#f9f9f9;
左边框:5px实心#ccc;
利润率:10px;
填充:0.5em10px;
}
-我无法重现问题代码可以这样重写,但它如何解决问题?就我通过测试代码所知,根本没有问题。你说得对@Quentin,我重构了我的代码示例,以展示如何将每个引号作为DOM元素的子元素附加,并带有quotesBox
id。