0

Can someone tell me what is wrong this code?

    $.ajax({
  "url":"https://en.wikipedia.org/w/api.php?",
  "dataType": "jsonp",
    "action": "opensearch",
    "format": "json",
    "search": "new york",
    "namespace": "0",
    "limit": "3",
    "formatversion": "1",

  success: function(response){
    console.log(response);
  }
});

Why do I get the following error message?

Refused to execute script from 'https://en.wikipedia.org/w/api.php?&callback=jQuery111105448874468459555_1518288921946&_=1518288921947' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

Thank you.

1

1 Answer 1

3

The following are not ajax parameters (instead they are api parameters):

"action": "opensearch",
"format": "json",
"search": "new york",
"namespace": "0",
"limit": "3",
"formatversion": "1",

You can pass those values as query string, changing your url from:

"url":"https://en.wikipedia.org/w/api.php?",

to:

"url":"https://en.wikipedia.org/w/api.php?"+ $.param(apiParams),

where the api parameter is:

var apiParams = {action: 'opensearch', search: 'new york', limit: 3, namespace: 0, formatversion: 1, format: 'json'};

or you can use the data parameter like:

data: apiParams,

The snippet:

var apiParams = {action: 'opensearch', search: 'new york', limit: 3, namespace: 0, formatversion: 1, format: 'json'};
$.ajax({
    "url":"https://en.wikipedia.org/w/api.php?" + $.param(apiParams),
    "dataType": "jsonp",
    //data: apiParams,
    success: function(response){
        console.log(JSON.stringify(response));
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.