如何将变量分配给链接中包含的信息?(Javascript/HTML)

How to assign a variable to information contained in a link? (Javascript/HTML)

本文关键字:信息 Javascript HTML 包含 变量 分配 链接      更新时间:2023-09-26

>我是一个初学者程序员,我有一个关于为链接中的信息分配变量的问题。我不能给出确切的例子,所以我会尝试想出一些东西来告诉你我的意思:

想象一下,有一堆链接,其中写的所有内容都是:

{"first_name": 约翰, "last_name": 多伊, "年龄": 37};

另一个链接将包含:

{"first_name": 乔娜, "last_name": 多娜, "年龄": 71};

没有别的。我无法控制链接,但我需要将这些对象分配给某个变量,以便我以后可以在我的某些函数中使用这些属性。

例如(第一个链接):变量 x=information_in_first_link;

这可能吗?我该怎么做?

提前感谢! :)

(编辑以使其更清晰...

如果有人感兴趣,我在这里找到了答案:http://www.w3schools.com/json/json_http.asp

var xmlhttp = new XMLHttpRequest();
var url = "myurl";
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        var example = JSON.parse(xmlhttp.responseText);
        myFunction(example);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(arr) {...}

了解到我所说的格式称为JSON。您可以在我在帖子开头发布的链接中了解更多信息。 :)