我从gltich中获取json数据。
当我转到那个链接时,url重定向到另一个域。
https://wind-bow.glitch.me/twitch-api/users/freecodecamp (域名从
gomix.me更改为glitch.me__)
对于这两个HTTPs请求,我都使用邮递员,得到相同的JSON数据。但是,在使用jQuery .getJSON方法时,两者产生的结果并不相同
$(document).ready(function() {
$.getJSON("https://wind-bow.gomix.me/twitch-api/users/freecodecamp", function(data1) {
console.log("Gomix.me URL", data1); // nothing outputted
});
$.getJSON("https://wind-bow.glitch.me/twitch-api/users/freecodecamp", function(data2) {
console.log("Glitch.me URL", data2); // expected data outputted
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
是否可以通过将被重定向的gomix.me获取JSON数据,或者我是否只能使用最终端点(仅使用glitch.me )
发布于 2017-12-31 22:35:50
Gomix请求被CORS策略阻止。您可以使用这个heroku应用程序绕过它:https://cors-anywhere.herokuapp.com/。
$(document).ready(function() {
$.getJSON("https://cors-anywhere.herokuapp.com/https://wind-bow.gomix.me/twitch-api/users/freecodecamp", function(data1) {
console.log("Gomix.me URL", data1); // now it works
});
$.getJSON("https://wind-bow.glitch.me/twitch-api/users/freecodecamp", function(data2) {
console.log("Glitch.me URL", data2); // expected data outputted
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
发布于 2017-12-31 22:44:50
如果您将$.ajax与dataType: 'jsonp'结合使用,它将对您有所帮助。
$(document).ready(function() {
$.ajax({
url: "https://wind-bow.gomix.me/twitch-api/users/freecodecamp",
type: 'GET',
dataType: 'jsonp',
success: function(data1) { console.log("Gomix.me URL", data1);}
});
});https://stackoverflow.com/questions/48046036
复制相似问题