Getting JSON from the URL [JavaScript/AJAX Code]

To request JSON from an URL using JavaScript/AJAX, you need to send an HTTP GET request to the server and provide the Accept: application/json request header with your request. The Accept header tells the server that our JavaScript/AJAX client is expecting JSON. The server informs the JavaScript/AJAX client that it has returned JSON with a Content-Type: application/json response header. In this JavaScript/AJAX JSON from URL example, we make a GET request to the ReqBin echo URL to get the JSON. Click Send to run JavaScript/AJAX Get JSON from URL example online and see results. The JavaScript/AJAX code was automatically generated for the GET JSON example.
Getting JSON from the URL [JavaScript/AJAX Code] Send
GET /echo/get/json HTTP/1.1
Accept: application/json

Updated: Viewed: 64768 times
JavaScript/AJAX code for GET JSON example

JavaScript/AJAX code for GET JSON Example

This JavaScript/AJAX code snippet was generated automatically for the GET JSON example.
<< Back to the GET JSON example

What it JSON?

JavaScript Object Notation (JSON) is a lightweight text-based, language-independent data exchange format. JSON defines a small set of formatting rules for the portable representation of structured data. JSON can represent four primitive types (strings, numbers, boolean values, and null) and two structured types (objects and arrays). JSON is used to exchange data between applications written in many programming languages, including JavaScript, Java, C++, C#, Go, PHP, Python, and many others.

How to request data in JSON format using JavaScript/AJAX?

To get data in JSON format from the server, the JavaScript/AJAX client must explicitly tell the server that it expects JSON by sending the Accept: application/json request header. Without the Accept header, the server may automatically send data in a different format that it thinks is best for the API client (based on the UserAgent header), and it might not be JSON.

JSON Accept Header Example
Accept: application/json

How to send and receive JSON data using JavaScript?

JSON data is transmitted over the network as a text string and must be converted to a JavaScript object before using it. Likewise, before sending JavaScript objects over the network in JSON format, they must be converted to strings. JavaScript has two built-in functions for converting JavaScript objects to JSON and vice versa:

  • JSON.parse() - converts a JSON string to a JavaScript object.
  • JSON.stringify() - converts a JavaScript object to a JSON string.

JavaScript/AJAX JSON Request Example

An example of an HTTP GET request to fetch JSON data from a ReqBin echo URL. Click Generate Code to see the JavaScript/AJAX code for this JSON request example.

JSON Request Example
GET /echo/get/json HTTP/1.1
Accept: application/json

The server response to our JavaScript/AJAX client's request.

JSON Response Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19


The server returned JSON in the body of the HTTP response message. The Content-Length: 19 header indicates the length of the JSON data in the response body, and the Content-Type: application/json response header indicates the type of data.


The official MIME type for JSON is application/json:

JSON Content-Type Header Example
Content-Type: application/json

See also

Generate code snippets for JavaScript/AJAX and other programming languages

Convert your GET JSON request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the JavaScript/AJAX code generator.

// try get IpInfo ASAP! getIpInfo($.noop);