2017-12-31 394 views
2

私はgltich APIからjsonデータを取得しています。javascriptリダイレクトされたURLへのAPI呼び出しでjsonデータを取得する方法

https://wind-bow.gomix.me/twitch-api/users/freecodecamp

私はそのリンクに行くと、URLが別のドメイン

https://wind-bow.glitch.me/twitch-api/users/freecodecamp

glitch.meからgomix.meから、ドメイン名の変更)

にリダイレクト

両方のHTTPリクエストにpostmanを使用すると、同じ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にリダイレクトされますURLからJSONデータを取得することが可能であり、あるいは私がすることができるだけです最終エンドポイントはglitch.meのみを使用しますか?

答えて

2

GomixリクエストはCORSポリシーでブロックされています。あなたはそれを迂回するためにこの英雄アプリを使うことができます: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>

+0

どうすればいいですか?callback =?代わりに私のhttps要求の最後に? – Kagerjay

1

あなたはdataType: 'jsonp'$.ajaxを使用している場合、それはあなたを助けます。

$(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);}  
    }); 
}); 
関連する問題