我正在尝试向 https://api.mojang.com/users/profiles/minecraft/USERNAME 发出 AJAX 请求,这应该返回 JSON 数据(不确定是否为 JSONP)
当我单击将执行 AJAX 请求的按钮时,我得到:
http://prntscr.com/8xswr1 (Google Chrome 控制台)
当我双击“dude1?callback...”时,我得到:
其中包含我想要返回的 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/