【问题标题】:Wordpress HTML AJAX JSON get data and parseWordpress HTML AJAX JSON 获取数据并解析
【发布时间】:2017-12-01 17:07:36
【问题描述】:

下面的代码中提供了一个与此链接相关联的 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>

【问题讨论】:

    标签: javascript jquery json ajax wordpress


    【解决方案1】:

    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
    });
    

    【讨论】:

    • 感谢我更新了我的 jQuery。并使用了getJSON。仍然无法正常工作。你有一个我可以用来做这个的例子吗?另外,我知道 Wordpress 有一些额外的钩子,你必须为 ajax 代码添加一些钩子。你有资料来源,我可以阅读需要做的事情吗?
    猜你喜欢
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    相关资源
    最近更新 更多