Wikipedia API returning a string, not JSON object


here is my project

when i try to run the ajax call to the wiki api i seem to be receiving a string instead of an object. Prior to this i was getting an error that was resolved when i removed “dataType: ‘json’” so im assuming that error was because the api was never sending me a json file in the first place? Can someone help me understand this a little more intimately. Why am i not receiving a json object? why am i not allowed to specify “dataType: ‘json’” without receiving this error: SyntaxError: Unexpected token / in JSON at position 0. Am i missing an important detail about how API’s and ajax requests work that i should read up on? Please share any documentation :slight_smile:
Here is my ajax request:

function userWiki(search){
var wikiEndPoint = ""
var url = “/w/api.php?action=opensearch&format=json&origin=*&search=” + search + “&warningsaserror=1&callback=data”;
url: wikiEndPoint + url,
success: function( data ) {
error: function(xhr, status, error) {
console.log("status: " + status);
console.log("error: " + error);
console.log("xhr: " + xhr);
cache: false //this is important


There are a couple of issues to fix in your codepen.

  1. remove the last two parameters of your URL after search. The first one warningsaserror=1 is not supported with action=opensearch and the second one callback=data is telling the API to send the data to a function called data (which does not exist) when the response is complete. In the end, your URL should be:
var url = "/w/api.php?action=opensearch&format=json&origin=*&search=" + search

  1. With a <form> element, the page gets refreshed when you submit data - so when you click the submit button in your form, the page refreshes and you lose the data you want to parse. To fix it, run your code when the form is submitted and use preventDefault() to prevent the refresh from happening:
  var searchContent = $("#srch-term").val();


  var searchContent = $("#srch-term").val();

This will have the added benefit of letting you use the Enter key to search without any additional code.