简体   繁体   English

我试图从html中的json获取数据。 我不想使用Ajax,因为

[英]I am trying to get the data from json in html. I don't want to use Ajax because

I ma learning XMLHttpRequest . 我正在学习XMLHttpRequest JSON data is not loading. JSON数据未加载。 For some reason I am not getting the value of my json data. 由于某种原因,我没有得到json数据的值。 My code 我的密码

function foo() {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', "data.json",true);
    httpRequest.send();
    var abc =  httpRequest.responseText;
}

My Json Data 我的Json资料

[
    {
        ID : 0,
        VALUE : "United State"
    },{
        ID : 1,
        VALUE : "United Kingdom"
    },{
        ID : 2,
        VALUE : "Afghanistan"
    },{
        ID : 3,
        VALUE : "Aland Islands"
    },{
        ID : 4,
        VALUE : "Albania"
    }
]

What I am making wrong here. 我在这里做错了。

You are missing an onreadystatechange event handler, which is how you actually get the data: 您缺少一个onreadystatechange事件处理程序,它实际上是如何获取数据的方式:

function foo() {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', "data.json",true);
    httpRequest.onreadystatechange = function () {
        if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
            console.log(httpRequest.responseText);
        }
    };
    httpRequest.send();
}

If you need to work with that data outside of your function, you can use the callback pattern: 如果您需要在函数之外使用该数据,则可以使用回调模式:

function foo(callback) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', "data.json",true);
    httpRequest.onreadystatechange = function () {
        if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
            // trigger your callback function
            callback(httpRequest.responseText);
        }
    };
    httpRequest.send();
}

foo(function(data) {
  console.log(data);
});

Or Promises : 承诺

function foo(callback) {
  return new Promise(function(resolve, reject) {
    var httpRequest = new XMLHttpRequest();
    httpRequest.open('GET', "data.json",true);
    httpRequest.onreadystatechange = function () {
        if(httpRequest.readyState === XMLHttpRequest.DONE && httpRequest.status === 200) {
            // trigger your callback function
            resolve(httpRequest.responseText);
        }
    };
    httpRequest.send();
  });
}

foo().then(function(data) {
    console.log(data);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法摆脱 html 底部的这个逗号。我正在使用 react 并在 index.html 中我没有看到它,也没有在我的任何组件中看到它 - Cannot get rid of this comma on the bottom of html. I am using react and in the index.html I don't see it there or in any of my components 我正在尝试使用我在 this.then() function 中获得的返回值。我不知道如何修复它 - I am trying to use the return value that I get in this .then() function. I don't know how to fix it 我想通过ajax调用从地图获取json数据 - i want to get json data from map through ajax call 我无法从使用Javascript(Ajax)的PHP得到答复。 虽然我可以在PHP页面上显示结果,但我希望将其重新显示在HTML页面上 - I can't get a reply from PHP using Javascript (Ajax). While I am able to display results on PHP page, I want it back on the HTML page 我不知道如何在html中显示此内容。 这个脚本有什么问题? - I don't know how to make this show within the html. what is wrong with this script? 我想导航到另一个 html 中的特定 div。 为什么这段代码不起作用? - I want navigate to particular div in another html. Why this code is doesn't work? 我正在尝试从用户输入中获取一些数据到 JSON 文件中。我收到此消息 - I am trying to get some data from user input into a JSON file.I get this message 我正在尝试使用js将多个功能和按钮连接在一起。 和html。 我不确定我缺少什么 - I am trying to connect multiple functions and buttons together using js. and html. and I'm not sure what i'm missing 我正在尝试从错误消息中获取文本并与预期内容进行比较,但由于没有正确的CSS路径或html id,它会返回“ null” - I am trying to get text from an error message and compare to the expected but it returns “null” since don't have proper css path or html id 我想在使用的模板上使用JQuery Datepicker,但是由于模板中集成了JQuery Core文件,所以我无法使其正常工作 - I want to use JQuery Datepicker on a template I used, but because of the integrated JQuery Core file from the template, I can't get it to work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM