1

I have tried every combination I could find on here and the Wikimedia API site, still I can't seem to get any data returned. This is my code,

$('#go').click(function () {
    var str = $("#input").val();
    var fullstr = 'https://en.wikipedia.org/w/api.php?format=json&action=query&list=search&srnamespace=0&srsearch=' + str + '&srprop=snippet&format=json&callback=json';
    $.getJSON(fullstr, function(data) {
      alert(data);
    });
  });

I have tested it up until the getJSON call and it seems to be working, but I don't seem to be getting anything from the call. (The final alert() is just to check for any object being returned).

What is the problem?

Thanks!

3
  • If you are using something like Chrome to debug - have you checked the Network tab in the debugger to see exactly what was sent and returned? Commented Nov 10, 2017 at 11:55
  • @F.Moss I think Its due to cross-origin header problem Commented Nov 10, 2017 at 12:05
  • Thank you, I'll look into these things. Commented Nov 17, 2017 at 13:37

2 Answers 2

2

It could be helpful to you, while framing a URL you need to pass origin= *

$(document).ready(function(){              
     $("#searchWiki").click(function(){
          var q = document.getElementById("searchid").value;
          $.getJSON("https://en.wikipedia.org/w/api.php?action=query&format=json&gsrlimit=15&generator=search&origin=*&gsrsearch=" + q, function(data){
     console.log(data)
     });
   });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <div id="search">                    
         <input id="searchid" class="input-lg" name="gsrsearch" type="text" placeholder="search Wiki" autocomplete="off"/>
         <button id="searchWiki" class="btn-lg btn-info">Search</button>                          
    </div>      

Sign up to request clarification or add additional context in comments.

Comments

1

You have to add the origin=* parameter in your request, to prevent the cross origin error. Something like this:

https://en.wikipedia.org/w/api.php?action=query&list=search&srnamespace=0&srsearch=' + str + '&srprop=snippet&format=json&callback=json&origin=*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.