Can t get json data in Wikipedia Viewer

Can t get json data in Wikipedia Viewer
0.0 0

#1

Hello,

I m trying to access some json data, to log it but it doesn t work no matter what, I tried several URL’s even from wikipedia’s examples and no result, nothing happens. I tested my code on submit, it gets the query but when it tries to grab json data it gets stuck there. This is my code so far:

$(document).ready(function(){
   $("#form").submit(function(){   
    var query = $("#query").val();
  
    var url = 'https://en.wikipedia.org/w/api.php?action=opensearch&search=' + query+ '&format=json&callback=?';
     
    $.getJSON(url, function(json){
      console.log(json);
    });
 });  
});
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div class="articles">
     <div class="random">
   <a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">Click here for a random article</a>
  </div>
  <form id="form" >
    <input type="search" id="query">
     <div class="close"></div>
    </form>
    
    <p>click icon to search</p>
  </div>

</body>
</html>

#2

Can you share the html too?


#3

This is the html for now i edited the post


#4

Thanks. One quick idea: do you have jquery in your project?


#5

yeah i m using this https://code.jquery.com/jquery-3.3.1.min.js and also this from bootstrap https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js.
I can console the value from my input so the issue is with json. I think it has to do with cross origin issue


#6

I added e.preventDefault(); after submit and now it works. Apparently this was the cause


#7

I added the action attribute to your input form, and now I can see the response for my query, but it looks like it’s coming back in JS, not XHR.

.

My guess is that you need to add the response data type in your api call. I’ll play with it for a bit longer and let you know if I find anything.


#8

Oh, great! You got it!


#9

thank you for your help


#10

Don’t know if I actually helped! But happy to try!


#11

Damn their API is horrible… so messed up