Wordpress HTML AJAX JSON获取数据和解析

时间:2017-12-01 17:07:37

标签: javascript jquery json ajax wordpress

下面的代码中提供了与此链接相关联的JSON文件。如何从中获取数据和Ajax更新数据?到目前为止,这就是我所拥有的。但是,按钮甚至不起作用,我甚至不确定我的编码是否正确以正确提取数据。对不起,我是这个编码的新手。

<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>

1 个答案:

答案 0 :(得分:0)

jQuery slim不支持AJAX。使用普通的jQuery:)

另外,请考虑使用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
});