简体   繁体   English

JSONP请求错误

[英]JSONP request error

I'm trying to make a request to this url: 我正在尝试对此网址发出请求:

https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=json https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=json

...using JSONP. ...使用JSONP。

The function I'm using to make this request is 我用来发出此请求的函数是

 <script>

 function foo(data)
 {
 var obj = JSON.parse(data);
 console.log(obj);

 }

 var script = document.createElement('script');
 script.src = 'https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=jsonp?callback=foo'

 document.getElementsByTagName('head')[0].appendChild(script);
 // or document.head.appendChild(script) in modern browsers

 </script>

When I load this function in google chrome I get 当我在谷歌浏览器中加载此功能时

 Refused to execute script from 'https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=jsonp?callback=foo' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.

... in the console. ...在控制台中。 How do I execute this request? 如何执行此请求? Thanks!!! 谢谢!!!

Try changing the src URL to the following: 尝试将src URL更改为以下内容:

https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=json&callback=foo https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=json&callback=foo

This should cause the API to send the appropriate JSONP response you are looking for. 这应该使API发送您正在寻找的适当的JSONP响应。

EDIT - Now with working code. 编辑 -现在使用工作代码。

 function foo(data) { console.log(data[0]); } var script = document.createElement('script'); script.src = 'https://en.wikipedia.org/w/api.php?action=opensearch&search=apple&limit=5&namespace=0&format=json&callback=foo' document.getElementsByTagName('head')[0].appendChild(script); // or document.head.appendChild(script) in modern browsers 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM