JSON数据成变量
edit :我解决了该错误,但是,它似乎仅打印出一个实例。我该如何使所有条目都进入该变量?
我对此有些新,但我正在尝试。抱歉,这个问题听起来很愚蠢。 我配置了JSON服务器,它可以正常工作。我可以成功地删除数据,但是,我收到的数据要进入一个变量,以便以后可以在另一个函数中使用它。为此,我创建了变量日期图。但是,它似乎不好。它似乎没有在每个循环之外读取它。有什么想法是什么? 我尝试在每个循环中使用console.log(),并且效果很好,但是它在它之外行不通。
function solicitare() {
adresa = "http://localhost:4000/listaexamene?callback=?";
$.getJSON(adresa, function (raspuns) {
$.each(raspuns, function (indice, examen) {
continutDeAfisat = "<div><i>Subiect: " + examen.examId + "</i>, Student: " + examen.studentId + "</div>";
$(continutDeAfisat).appendTo("#datenoi");
var dategraph = {};
dategraph.examId = examen.examId;
dategraph.studentId = examen.studentId;
});
console.log(dategraph);
stringNou = JSON.stringify(dategraph);
console.log(stringNou);
});
}
我收到的错误如下:
EDIT: I solved that error, however, it only seems to print out just one instance. How can I make it put all the entries into that variable?
I'm a little bit too new to this, but I'm trying. Sorry if the question sounds stupid.
I've configured a JSON server and it works just fine. I can pull out the data successfully, however, the data I receive I'd like to go into a variable so that I can use it later on in another function. For this purpose, I created the variable dategraph. However, it doesn't seem to work good. It doesn't seem to read it outside the $each loop. Any ideas what could it be?
I tried using console.log() in the $each loop and it works fine, however, it won't work outside of it.
function solicitare() {
adresa = "http://localhost:4000/listaexamene?callback=?";
$.getJSON(adresa, function (raspuns) {
$.each(raspuns, function (indice, examen) {
continutDeAfisat = "<div><i>Subiect: " + examen.examId + "</i>, Student: " + examen.studentId + "</div>";
$(continutDeAfisat).appendTo("#datenoi");
var dategraph = {};
dategraph.examId = examen.examId;
dategraph.studentId = examen.studentId;
});
console.log(dategraph);
stringNou = JSON.stringify(dategraph);
console.log(stringNou);
});
}
The error I receive is the following:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在声明内部范围中的变量。因此,外部范围不知道该变量是什么。尝试:
使用代替。
避免使用
var
,然后 不会对你很满意。声明新变量时,请始终使用让
或const
。使用
const
当您 wont 将值重新分配给变量时。使用
Let
将来将 will 将值重新分配给一个变量。本地箭头功能。而不是这样做:
您可以这样做:
通常不想这样做的唯一一次是在JavaScript中使用
此
关键字时。未使用的本地变量 IE
Indice
在您的情况下,由于您不使用它。人们通常会_
表明它是未使用的变量。因此,您可以使用do_indice
或_
,甚至更容易,只需do(,examen)
。字符串插值。如您所见,做
“&lt; div&gt;&lt; i&gt;主题非常烦人:而不是
“”
用``
制作字符串。然后,您可以使用类似$ {evign.examid}
的变量进行字符串插值。这就是我要做的。
如果您在VS代码中编写JavaScript,我还可以建议安装一个称为
Prettier
的扩展程序,该扩展名将有助于格式化您的代码并使其更可读。新问题:我如何使它从循环中保存所有答案,而不仅仅是一个?
尝试以下操作:
首先,制作
dategraph
阵列。然后,我们将每个结果对象推入数组。这样的事情:You're declaring the variable in an inner scope. So the outer scopes don't know what that variable is. Try:
Avoid using
var
and uselet
instead.Simply doing
myNewVariable = {};
will work, but javascript will not be very happy with you. When declaring new variables always uselet
orconst
.Use
const
when you wont re-assign a value to a variable.Use
let
when you will re-assign a value to a variable in the future.Local Arrow Functions. Instead of doing:
You can do:
The only time you generally don't want to do this, is when you are working with the
this
keyword in javascript.Unused local variables i.e.
indice
in your case, since you aren't using it. People usually do_
to indicate that it's an unused variable. So you can use either do_indice
or_
, or even easier, just do(, examen)
.String interpolation. As you can see, its pretty annoying to do
"<div><i>Subject:" + examen.examId + "...."
. Instead of""
make the string with``
. Then you can do string interpolation with variables like this${examen.examId}
.This is what I'd do.
If you write your javascript in VS code, I can also recommend to install an extension called
prettier
which will help format your code and make it more readable.New question: How can I make it that it saves all the answers from the loop and not just one?
Try this:
First, make
dategraph
an array. Then we push each result object into the array. So something like this: