我必须做一些非常简单的事情,但据我所知,似乎没有一种简单的方法可以做到这一点.我只想从远程源加载JSON数据,并使用jQuery将其存储在全局Javascript变量中.这就是我所拥有的:
var my_json;
$.getJSON(my_url, function(json) {
var my_json = json;
});
Run Code Online (Sandbox Code Playgroud)
my_json变量仍未定义.我认为这显然是一个范围问题.在我看来,$ .getJSON方法应该返回JSON,但它返回一个XMLHttpRequest对象.如果我这样做:
request = $.getJSON(my_url);
my_json = request.responseText.evalJSON();
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为在readystate == 4之前,responsetext保持为null.看来你必须使用回调函数来返回响应文本,因为它会在成功时触发.
这不可能是这么难!对?
我是Angular的新手.我需要使用AngularJS 1.6从JSON文件渲染动态内容.这就是我所拥有的.
News.json
{
"Articles": [
{
"Title": "News 1",
"Abstract": "News 1 abstract ...",
"Body": "News 1 starts here.... ",
"Comments": [
{
"comment 1" : "Comment 1 goes here",
"comment 2" : "Comment 2 goes here",
"comment 3" : "Comment 3 goes here"
}]
},
{
"Title": "News 2",
"Abstract": "News 2 abstract ... ",
"Body": "News 2 starts here...",
"Comments": [
{
"comment 1" : "Comment 1 goes here",
"comment 2" : "Comment 2 goes here"
}] …Run Code Online (Sandbox Code Playgroud)