简体   繁体   English

如何使用jQuery调用JSONP?

[英]How to call a jsonp using jquery?

I have this api http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&convo_id=exampleusage_1231232&format=xml which I call using the browser then I am getting proper response. 我有这个api http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&convo_id=exampleusage_1231232&format=xml ,我使用浏览器对其进行了调用,然后得到了正确的响应。 But when I call using jquery ajax then I am getting the error 但是当我使用jquery ajax打电话时我得到了错误

*Refused to execute script from 'http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&c…ormat=xml&callback=?%20&callback=jQuery172005527849208121283_1460880216789' because its MIME type ('text/xml') is not executable, and strict MIME type checking is enabled.*

code is as below 代码如下

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>JQuery (cross-domain) JSONP Twitter example</title>
        <script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
        <script>
            $.ajax({
    url: "http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&convo_id=exampleusage_1231232&format=xml&callback=? ",
    type: "GET",
    dataType: 'jsonp',
    cache: true,
    success: function (data, status, error) {
      console.log('success', data);
    },
    error: function (data, status, error) {
      console.log('error', data, status, error);
    }
});
        </script>
    </head>
    <body>
        <ul id="gists"></ul>
    </body>
</html>

Can anybody please tell me why I am doing wrong? 有人可以告诉我为什么我做错了吗?

Your response is a XML file. 您的回复是一个XML文件。 You should change the format query to json . 您应该将format查询更改为json

Here is the response in JSON: 这是JSON中的响应:

{"convo_id":"exampleusage_1231232","usersay":"WHAT IS YOUR NAME","botsay":"My name is Program-O."}

The problem is here 问题在这里

$.ajax({
    url: "http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&convo_id=exampleusage_1231232&format=xml&callback=? ",
    type: "GET",
    dataType: 'jsonp',

You're requesting for XML 您正在请求XML

http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&convo_id=exampleusage_1231232&format=xml

If you replace XML with JSON it should work. 如果将XML替换为JSON它应该可以工作。

http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name& convo_id=exampleusage_1231232&format=json

When u try to make a direct call to http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&convo_id=exampleusage_1231232&format=json in your browser, you will notice the return headers are Content-Type:text/plain; charset=utf-8 当您尝试在浏览器中直接致电http://api.program-o.com/v2/chatbot/?bot_id=6&say=what%20is%20your%20name&convo_id=exampleusage_1231232&format=json时,您会注意到返回的信息标头是Content-Type:text/plain; charset=utf-8 Content-Type:text/plain; charset=utf-8 and X-Content-Type-Options:nosniff . Content-Type:text/plain; charset=utf-8X-Content-Type-Options:nosniff It tells the Chrome to have a strict MIME type check. 它告诉Chrome浏览器进行严格的MIME类型检查。 Can you try change the server output Content-Type to application/javascript or remove the X-Content-Type-Options:nosniff ? 您可以尝试将服务器输出的Content-Type更改为application/javascript还是删除X-Content-Type-Options:nosniff

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

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