Search.../

getJSON( )

Retrieves the specified JSON resource from the network using HTTPS.

Description

The getJSON() function retrieves a JSON resource from the network. It returns a Promise that resolves to a JSON object representing the response to the request.

To use getJSON, import it from wix-fetch:

import {getJSON} from 'wix-fetch';
javascript | Copy Code

Retrieving the JSON object is performed using the GET method, regardless of what is specified in the options parameter.

The Accept header is assumed to be application/json by default, but you can override it by explicitly setting a different value for Accept.

The optional WixFetchRequest object contains information about an HTTPS request. Additional functionality is available in each of the respective Fetch API implementations.

Notes: Some common errors when using the getJSON() function are described here along with possible solutions.

  • You can usually tell if you are experiencing these issues by checking your browser's console using the browser's developer tools.
  • Certain CORS (Cross-Origin Resource Sharing) requests are restricted when originating from a browser. Usually, GET requests and certain POST requests can be made from your site's Public, Page, or Site code. All other requests need to be made from your site's Backend code. If you are experiencing an issue with a getJSON() call due to a CORS restriction, move the getJSON() call to the backend as described in Accessing 3rd-Party Services.
  • You cannot request HTTP content if your site is an HTTPS site. To fix this issue you can either use the HTTPS protocol to fetch the requested resources or you can turn off SSL on your site.

Syntax

function getJSON(url: string, [options: WixFetchRequest]): Promise<Object>

getJSON Parameters

NAME
TYPE
DESCRIPTION
url
string

The url of the JSON resource to retrieve.

options
Optional
WixFetchRequest

Options for the retrieval operation.

Returns

Fulfilled - The JSON response of the fetch operation. Rejected - The error that caused the rejection.

Return Type:

Promise<Object>

Was this helpful?

Get a JSON resource

Copy Code
1import {getJSON} from 'wix-fetch';
2
3// ...
4
5getJSON("https://someapi.com/api/someendpoint")
6 .then(json => console.log(json.someKey))
7 .catch(err => console.log(err));
Get a JSON resource

In this example, we demonstrate how you can you can get data from a resource using getJSON(). You can test out the code in our example template.

Copy Code
1import { getJSON } from 'wix-fetch';
2
3// GET call using getJSON
4export async function getGreetings() {
5 const response = await getJSON('https://velo-examples.wixsite.com/training-tester/_functions/greetings');
6 return response;
7}
8