# API - getJSON problem

**URL:** <https://forum.freecodecamp.org/t/api-getjson-problem/152951>\
**Category:** General\
**Created:** [October 13, 2017, 11:45am UTC](https://forum.freecodecamp.org/t/api-getjson-problem/152951 "2017-10-13T11:45:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Strzesia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/strzesia/32/67236_2.png) [@Strzesia](https://forum.freecodecamp.org/u/Strzesia)\
**Post date:** [October 13, 2017, 11:45am UTC](https://forum.freecodecamp.org/t/api-getjson-problem/152951/1 "2017-10-13T11:45:27Z")

</div>

Hi, guys.  
I’m having a problem with my Wikipedia Viewer challenge. When I make JSON request, nothing happens. I tried a few things, but I’m not able to find my mistake. The only thing I know is that the problem appears with getJSON - console log works before that.  
I could really use some help.  
My codepen: [https://codepen.io/Strzesia/pen/QqxoXx](https://codepen.io/Strzesia/pen/QqxoXx)

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [October 13, 2017, 12:34pm UTC](https://forum.freecodecamp.org/t/api-getjson-problem/152951/2 "2017-10-13T12:34:14Z")

</div>

You can add `origin=*` in the URL parameters (not all APIs do this, but Wikipedia’s does):  
`https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json&origin=*`

---

<div class="post-metadata">

**Author:** ![Strzesia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/strzesia/32/67236_2.png) [@Strzesia](https://forum.freecodecamp.org/u/Strzesia)\
**Post date:** [October 13, 2017, 12:59pm UTC](https://forum.freecodecamp.org/t/api-getjson-problem/152951/3 "2017-10-13T12:59:27Z")

</div>

Thank you so, sooo much! I was afraid it will never work. How should it look if the request is made by $.ajax?

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [October 13, 2017, 1:09pm UTC](https://forum.freecodecamp.org/t/api-getjson-problem/152951/4 "2017-10-13T13:09:57Z")

</div>

At the bare minimum, nothing much changes:

```auto
$.ajax({
  url: 'https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json&origin=*',
  success: function (data) {
    alert('help');
    console.log(data);
  }
});

```

But what I like about `.ajax` is you can separate the parameters from the URL:

```javascript
$.ajax({
  url: 'https://en.wikipedia.org/w/api.php',
  data: {
    action: 'query',
    titles: 'Main%20Page', // or encodeURIComponent('Main Page')
    prop: 'revision',
    rvprop: 'content',
    format: 'json',
    origin: '*'
  },
  success: function (data) {
    alert('help');
    console.log(data);
  }
});

```

It’s more lines, but the parameters are much more visible, and especially cleaner if you have to compute for the values somehow (like when you have to use `encodeURIComponent`), or if the values come from variables.

---

<div class="post-metadata">

**Author:** ![Strzesia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/strzesia/32/67236_2.png) [@Strzesia](https://forum.freecodecamp.org/u/Strzesia)\
**Post date:** [October 13, 2017, 1:22pm UTC](https://forum.freecodecamp.org/t/api-getjson-problem/152951/5 "2017-10-13T13:22:52Z")

</div>

Thanks again! I meant the second option, the one with separating the parameters - it also looks much more visible to me. In previous projects I used only getJSON, but I think now, when I can finally get any response, I’ll try something different 🙂

---

<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:** [January 17, 2021, 8:37pm UTC](https://forum.freecodecamp.org/t/api-getjson-problem/152951/6 "2021-01-17T20:37:39Z")

</div>

Closed on January 17, 2021, 8:37pm UTC


