[英]How do I get JSON data from a server into a javascript variable
Something so trivial and I just can't seem to find an answer. 如此琐碎的事,我似乎找不到答案。 This is my code so far. 到目前为止,这是我的代码。 However this is with the JSON alredy set as a variable. 但这是将JSON alredy设置为变量的。 I need it so that it gets the JSON text from 我需要它以便从中获取JSON文本
http://localhost:3001/sync/
and makes it equal to var txt. 并使其等于var txt。 INSTEAD of the var txt = I want it so get the JSON text from the URL INSTEAD of the text I've added. var txt的INSTEAD =我想要它,因此从我添加的文本的URL INSTEAD中获取JSON文本。
var txt = '{"loading":false,"playing":true,"position":0,"duration":389492,"index":13,"repeat":false,"shuffle":false,"volume":0.337499052286148,"context":{"uri":"spotify:user:@:playlist:66HXOPaG8wwe7k8t4YZj5b"},"contexts":[{"index":13,"descriptor":{"type":"list","uri":"spotify:user:@:playlist:66HXOPaG8wwe7k8t4YZj5b"}}],"track":{"artists":[{"image":"spotify:image:15a09a886f2149909821763f2f074cf1b7975574","images":[[64,"spotify:image:1aa2b5417668fdfc6966c9745b437e587d7ff23f"],[300,"spotify:image:15a09a886f2149909821763f2f074cf1b7975574"],[600,"spotify:image:865b8c83601ce2aef204a9c071fd2f531c12c000"],[1000,"spotify:image:5311029c2ba3de0b4e5d117b4e90d57b60720902"]],"name":"Duke Dumont","uri":"spotify:artist:61lyPtntblHJvA7FMMhi7E"}],"disc":1,"duration":389000,"image":"spotify:image:6f592ef177e159c00dd4f08049c4c962466b0776","images":[[64,"spotify:image:68fd12e77d374e7b9618ca0cf6786b9479837175"],[300,"spotify:image:6f592ef177e159c00dd4f08049c4c962466b0776"],[600,"spotify:image:6a30d6808f92167b4cb10eed2cf5f9838442d591"]],"name":"The Giver - Original Mix","number":2,"playable":true,"popularity":64,"starred":false,"explicit":false,"availability":"premium","album":{"uri":"spotify:album:66Io82H9e3b2rrtHFs2sE0"},"local":false,"advertisement":false,"placeholder":false,"uri":"spotify:track:6GbLDdBuFxZLDHhluGrrmA"}}';
var obj = eval ("(" + txt + ")");
document.getElementById("demo").innerHTML =
obj.playing;
It looks like you need to do use xmlhttprequest (ie. ajax). 看来您需要使用xmlhttprequest(即ajax)。 It is used to retrieve data from urls asynchronously. 它用于从URL异步检索数据。
You can either follow a tutorial to learn how to use it. 您可以按照教程学习如何使用它。 Or you could learn how to use jquery (specifically jquery.get). 或者,您可以学习如何使用jquery(特别是jquery.get)。
I'd suggest that if you don't know either, you either way start learning about xmlhttprequest but end up using jquery anyway. 我建议,如果您都不知道,则无论哪种方式都可以开始学习xmlhttprequest,但无论如何最终还是要使用jquery。
And as the other post mentioned, once you learn how to retrieve data, use JSON.parse to parse the text to json, even though jquery can handle that automatically, too. 就像另一篇文章提到的那样,一旦学习了如何检索数据,就可以使用JSON.parse将文本解析为json,即使jquery也可以自动处理它。
As far as I understand, u want the data to come from the URL and get assigned to variable txt. 据我了解,您希望数据来自URL并分配给变量txt。 In tat case you need to make a ajax call like this using jquery. 在tat情况下,您需要使用jquery进行这样的ajax调用。
$.ajax({
url:"http://localhost:3001/sync/",
success:function(result){
var txt = result;
}});
U can now use JSON.parse(txt) to parse the json. 您现在可以使用JSON.parse(txt)解析json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.