Home > Web Front-end > JS Tutorial > How to Retrieve JSON Data from URLs in JavaScript?

How to Retrieve JSON Data from URLs in JavaScript?

DDD
Release: 2024-10-31 06:27:30
Original
601 people have browsed it

How to Retrieve JSON Data from URLs in JavaScript?

Retrieving JSON Data from URLs in JavaScript

To access JSON data from a URL in JavaScript, you can employ various methods. One approach is to use jQuery's .getJSON() function:

$.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) {
    // JSON result in `data` variable
});
Copy after login

This function retrieves and parses the JSON response asynchronously, providing the results in the data parameter of the callback function.

For a purely JavaScript-based solution, you can utilize the fetch() API:

fetch('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')
  .then(response => response.json())
  .then(data => {
    // JSON result in `data` object
  });
Copy after login

In this method, the fetch() request returns a Promise, which, when resolved, contains the JSON response. Using .then(), you can extract the JSON data and perform any necessary operations.

The above is the detailed content of How to Retrieve JSON Data from URLs in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template