Issues with Wikipedia API Request [Solved]

Hey everyone,

I’m having some problems retrieving data from the Wikipedia API. The code returns results roughly once out of every 10 requests. My script is as follows:

function getSearchResults(data) {
  console.log(data);
  }

$("#submit").click(function(){
  var searchTerm = $("input[name='search']").val()
  var url = "http://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=" + searchTerm + "&callback=?";
  $.ajax({
    type: "GET",
    url: url,
    async: true,
    dataType: "jsonp",
    success: function(data) {
      getSearchResults(data);
    }
  })
});

When it fails:

Googling “307 Internal Redirect” hasn’t returned anything useful so far. Most Stack Overflow answers on the subject suggest disabling Chrome extensions, which I’ve done, yet the issue remains.

Strangest thing is, it works sometimes and not others. I don’t understand why I’m getting such inconsistent results. If anyone has any ideas, I’d really appreciate it.

Codepen

I think this is the thing
<form> have self native behaivor about sending data, and i think it is sync operation
so there actually happens two sending operation, and from this, ur form native behaivor “overlap” or mask ur ajax request…

Add e.preventDefault();

$("#submit").click(function(e){
  e.preventDefault();
5 Likes

That did it! Returns results every time now :grinning:

Would have never gotten that on my own, thank you so much!

1 Like

I battled with this issue for hours before finding this thread. Sigh. Thanks. I couldn’t figure it out, but it was definitely the enclosing form that was getting in the way, lol.

1 Like

I’m so happy I found this thread! I had no idea what was going on! my ajax requests were being ‘cancelled’. Thank you so much!

1 Like

I am so tremendously thankfull for your answer. I can not tell you how many days I have been looking at my code,
and how many different ajax requests I have send. I only got data returned once! I was just so desparate that I copied your code and tried it without the slighthest hope!!! :wink:

1 Like

Thanks for this! I was breaking my head trying to understand what was going on with the inconsistent results. I was beginning to think that the Wikipedia API had some problem with it.

1 Like

I have been having the same problem, however nameToReachPeople’s solution didn’t work for me, any other ideas as to what could be going on?