简体   繁体   English

Wordpress HTML AJAX JSON获取数据并解析

[英]Wordpress HTML AJAX JSON get data and parse

There is a JSON file that is associated with this link provided in the code below. 下面的代码中提供了与此链接关联的JSON文件。 How can I get the data from it and Ajax update data? 我如何从中获取数据以及Ajax更新数据? So far this is what I have. 到目前为止,这就是我所拥有的。 However, the button doesnt even work and i'm not even sure my coding is correct to pull the data correctly. 但是,该按钮甚至无法工作,我什至不确定我的编码是否正确才能正确提取数据。 Sorry, i'm new with this coding stuff. 抱歉,我是这个编码的新手。

 <html> <head> <metacontent=”text/html; charset = ISO-8859-1″http-equiv=”content-type”> <script type="text/javascript" src="/js/jquery-3.2.1.slim.min.js"></script> <script> function loadJSON(){ var data_file=”https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo;; var http_request =newXMLHttpRequest(); try{ // Opera 8.0+, Firefox, Chrome, Safari http_request =newXMLHttpRequest(); }catch(e){ // Internet Explorer Browsers{ try{ http_request =newActiveXObject(“Microsoft.XMLHTTP”); }catch(e){ // Something went wrong alert(“Your browser broke!”); returnfalse; } } } http_request.onreadystatechange =function(){ if(http_request.readyState ==4){ // Javascript function JSON.parse to parse JSON data var jsonObj = JSON.parse(http_request.responseText); // jsonObj variable now contains the data structure and can // be accessed as jsonObj.name and jsonObj.country. document.getElementById(“open”).innerHTML = jsonObj.open; document.getElementById(“high”).innerHTML = jsonObj.high; } } http_request.open(“GET”, data_file,true); http_request.send(); } </script> <title>tutorialspoint.com JSON</title> </head> <body> <h1>Stock details</h1> <tableclass=”src”> <tr><th>Open</th><th>High</th></tr> <tr><td>123.4 </td> <td>150.87 </td></tr> </table> <button type="button" onclick="loadJSON();">Update Details </button> </body> </html> 

jQuery slim doesn't support AJAX. jQuery slim不支持AJAX。 Use normal jQuery :) 使用普通的jQuery :)

Also, consider using getJSON 另外,考虑使用getJSON

$.getJSON('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=1min&apikey=demo', function (data) {
    console.log(data);
    //do stuff with your data
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM