API Wikipedia Viewer : Failed to load for <script> element

API Wikipedia Viewer : Failed to load for <script> element
0

#1

Hey,
I’m fighting ( :slight_smile: ) with Wikipidia Viewer and I really don’t understand why my request doesn’t work, i’m really confused :

$(document).ready(function(){

  $('form').submit(function() {
    var keyWord = $("#input-search").val();
    console.log(keyWord);

    $.ajax( {
      method: 'GET',
      url: "https://en.wikipedia.org/w/api.php",
      dataType : 'jsonp',
      headers : { 'Api-User-Agent': 'Example/1.0' },
      data: {
        action: "opensearch",
        format: "json",
        origin: "*",
        search: keyWord,
        formatversion: "2"
      },
      success: function(data) {
        console.log("success");
        console.log(data);
      },
      error: function(response) {
        console.log("error");
        console.log(response);
      }
    });
  });
});

I have this message in console :

You can check my project here : Api Viewer codepen

Thank you for your help,

Alexandre


#2

The form is submitting data to some index.html. You don’t really want to load a new page when you submit the form (since you want the data to display on the same page). You can add an event parameter in the submit event handler, then call event.preventDefault() to prevent that behavior.

$('form').submit(function(event) {
  event.preventDefault();
  // ... rest of the code ...
});

You’ll then hit another error. Your code is getting the value of some input with the input-search id, but there’s no such input element in your form.


#3

About “input-search” I forgot to copy my changes in local to codepen HTML section. Sorry about that.

Thank you for your help !!

I didn’t know “event.preventDefault();” I will loonking for more informations about it. Now I have a success return !

Thanks a lot !