简体   繁体   English

我如何从服务器获取JSON数据到javascript变量中

[英]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.

相关问题 如何从JavaScript变量检索JSON数据 - How do I retrieve json data from a javascript variable 如何从JavaScript中的JSON获取数据? - How do I get data from JSON in JavaScript? 如何将JSON数据从http.get(//从服务器json)分配给Javascript变量? - How to assign JSON data from http.get(//json from server) to Javascript variable? 如何从JavaScript获取变量并将其在服务器端(C#)中使用? - How do I get a variable from JavaScript and use it in the Server side (C#)? 在JavaScript代码中如何从Python JSON变量中获取数据 - In JavaScript code how to get data from a Python JSON variable 如何将我从 JavaScript 中的提取请求中获得的数据保存到我的 function 外部的变量中 - How do I save the data I get from a fetch request in JavaScript into a variable that is external to my function JavaScript将数据从json获取到全局变量 - JavaScript get data from json to a global variable 如何使用 Javascript 从服务器获取特定的 JSON 数据? - How to GET specific JSON data from a server using Javascript? 如何使用 JavaScript 从 IBM Cloud 通过 HTTP 请求获取 JSON 格式的数据? - How do I get data as JSON format from the IBM Cloud with a HTTP Request using JavaScript? 如何从 JSON 文件中获取隐藏变量? - How do I get a buried variable from a JSON file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM