javascript - jQuery JSON AJAX 请求不同域

标签 javascript jquery json ajax

我正在尝试向 https://api.mojang.com/users/profiles/minecraft/USERNAME 发出 AJAX 请求,这应该返回 JSON 数据(不确定是否为 JSONP)

当我单击将执行 AJAX 请求的按钮时,我得到:

http://prntscr.com/8xswr1 (Google Chrome 控制台)

当我双击“dude1?callback...”时,我得到:

http://prntscr.com/8xsx7q

其中包含我想要返回的 JSON 数据,这意味着我正在发出请求并获取 JSON 数据,但无法解析它或获取信息

代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Website</title>
        <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
        <script>
            function getInfo() {
                username = $('#username').val();
                $.ajax({
                    type: 'GET',
                    url: "https://api.mojang.com/users/profiles/minecraft/" + username,
                    //crossDomain: true,
                    dataType: 'jsonp',
                    success: function (data) {
                        alert(data.name);
                    }
                });
            }
        </script>
    </head>
    <body>
        <form>
            Username: <input type="text" id="username" name="username"/>
        </form>
        <button onclick="getInfo();">Submit</button>
        <span id="info">

        </span>
    </body>
</html>

此外,如果我将“dataType”参数更改为“json”,则会收到此错误 http://prntscr.com/8xsy9e

最佳答案

this should return JSON data (not sure if JSONP)

这是 JSON。

When I click on the button which will perform the AJAX request I get this

这是尝试像 JSONP 一样执行 JSON 时的典型错误。

When I double click on the 'dude1?callback...' I get this

是的,JSON。不是 JSONP。

Also, if I change the 'dataType' parameter to 'json' I get this error

是的,您请求数据的网站未授予您的网站使用访问者浏览器读取数据的权限。

<小时/>

跨源 Ajax 要求您从中获取数据的站点与您合作提供数据。

改为使用您的服务器获取数据。

关于javascript - jQuery JSON AJAX 请求不同域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33463822/

相关文章:

javascript - JQuery 命令互相破坏

php - 我是否需要在加载了 ajax 的 PHP 页面中再次包含 PHP 脚本?

ios - Objc live Json解析仅显示1项错误

javascript - 为什么 Prop 显示未定义?

javascript - 导航栏(下拉菜单)无法正常工作?

javascript - 如何在 chrome 扩展中动态运行后台脚本?

javascript - 动态禁用 ng-model

javascript - 在 backbonejs View 中,keypress/keyup/keydown 事件是否仅适用于输入而不适用于其他元素?

javascript - 如何根据Javascript是否启用采取不同的方法?

json - Groovy 文件检查