API - getJSON problem

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

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=*

1 Like

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

At the bare minimum, nothing much changes:

$.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:

$.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.

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 :slight_smile: