# Request for JSON content from URL

**URL:** <https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922>\
**Category:** General\
**Created:** [February 27, 2020, 10:40am UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922 "2020-02-27T10:40:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vokofe](https://avatars.discourse-cdn.com/v4/letter/v/d78d45/32.png) [@Vokofe](https://forum.freecodecamp.org/u/Vokofe)\
**Post date:** [February 27, 2020, 10:40am UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922/1 "2020-02-27T10:40:45Z")

</div>

Hi,

I am new to Javascript

I am working on an assignment where I’m suppose to request for JSON content.

================

let OrgURL =’’;

console.log(‘hi’);

function sendJSON(){

console.log(‘sendJSON’);

// Creating a XHR object

let xhr = new XMLHttpRequest();

// open a connection

xhr.open(“POST”, OrgURL, true);

// Set the request header i.e. which type of content you are sending

xhr.setRequestHeader(“Content-Type”, “application/json”);

// Create a state change callback

xhr.onreadystatechange = function () {

if (status === 0 || (200 \<= status && status \< 400))

```
    {

```

// Print received data from server

result.innerHTML = this.responseText;

console.log(xhr.responseText);

```
    } else {

```

// Oh no! There has been an error with the request!

```
    }

}; 

```

}

sendJSON();

================

I am expecting to get some kind of JSON object but nothing is showing.

How do I request for JSON content?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![Vokofe](https://avatars.discourse-cdn.com/v4/letter/v/d78d45/32.png) [@Vokofe](https://forum.freecodecamp.org/u/Vokofe)\
**Post date:** [February 27, 2020, 10:41am UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922/2 "2020-02-27T10:41:47Z")

</div>

> [@Vokofe](#):
>
> OrgURL

Please note there is a url but due to forum rules, i cannot show it.

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [February 27, 2020, 10:48am UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922/3 "2020-02-27T10:48:16Z")

</div>

This is rather outdated way of sending such request, consider using browser’s Fetch API:

> **[Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)**
>
> The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest, but the new API provides a more powerful and flexible feature set.

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [February 27, 2020, 10:50am UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922/4 "2020-02-27T10:50:00Z")

</div>

Also if you want to request something from the server, `GET` would be more appropriate method to do so

---

<div class="post-metadata">

**Author:** ![Vokofe](https://avatars.discourse-cdn.com/v4/letter/v/d78d45/32.png) [@Vokofe](https://forum.freecodecamp.org/u/Vokofe)\
**Post date:** [February 27, 2020, 2:36pm UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922/5 "2020-02-27T14:36:17Z")

</div>

Thanks for the info!

I will look up on Fetch()!

---

<div class="post-metadata">

**Author:** ![lendoo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lendoo/32/177827_2.png) [@lendoo](https://forum.freecodecamp.org/u/lendoo)\
**Post date:** [February 27, 2020, 6:03pm UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922/6 "2020-02-27T18:03:17Z")

</div>

Hi,  
sendJson() will nothing showing because the ajax call is works asyncronously.  
Change your function to assyncron function and will work well.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:35pm UTC](https://forum.freecodecamp.org/t/request-for-json-content-from-url/353922/7 "2021-06-01T19:35:00Z")

</div>


