有人能告诉我这段代码有什么问题吗?
$.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);
}
});
为什么会收到以下错误消息?
拒绝执行来自' https://en.wikipedia.org/w/api.php?&callback=jQuery111105448874468459555_1518288921946&_=1518288921947'的脚本因为它的MIME类型(' text / html')不可执行,并且启用了严格的MIME类型检查。
谢谢。
答案 0 :(得分:3)
以下不是ajax参数(而是api参数):
"action": "opensearch",
"format": "json",
"search": "new york",
"namespace": "0",
"limit": "3",
"formatversion": "1",
您可以将这些值作为查询字符串传递,从而更改您的网址:
"url":"https://en.wikipedia.org/w/api.php?",
为:
"url":"https://en.wikipedia.org/w/api.php?"+ $.param(apiParams),
其中api参数为:
var apiParams = {action: 'opensearch', search: 'new york', limit: 3, namespace: 0, formatversion: 1, format: 'json'};
或者您可以使用数据参数,如:
data: apiParams,
摘录:
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>