Understanding the Issue
The provided URL returns JSON data, but an attempt to retrieve it using readJsonFromUrl failed. The goal is to obtain a JavaScript object that represents the JSON response.
Solution using jQuery
One efficient method to retrieve JSON data in JavaScript is through the jQuery $.getJSON() function:
<code class="javascript">$.getJSON('http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback', function(data) { // Access the JSON data in the `data` variable });</code>
Alternative Pure JavaScript Solution
If you prefer not to use jQuery, consider this pure JavaScript solution:
<code class="javascript">var request = new XMLHttpRequest(); request.open('GET', 'http://query.yahooapis.com/v1/public/yql?q=select%20%2a%20from%20yahoo.finance.quotes%20WHERE%20symbol%3D%27WRC%27&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys&callback'); request.onreadystatechange = function() { if (request.readyState == 4 && request.status == 200) { var data = JSON.parse(request.responseText); // Access the JSON data in the `data` variable } }; request.send();</code>
The above is the detailed content of How to Retrieve JSON Data from a URL in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!