크로스 도메인 Ajax 응답 실패
-
21-08-2019 - |
문제
좋아 ... 그래서 나는이 대구 조각과 함께 꽤 오랫동안 돌아 다니고 있었다 ...
먼저 나는이 코드를 사용했다 ...
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'data.json',
dataType: "json",
success: function(data) {
$('#content').append(''+data.rank+'
');
}
});});
이 코드 (It Works) data.json에서 JSON 데이터는이 형식으로 포함됩니다.
{ "user_id": "3190399", "user_name": "anand_dasgupta", "followers_current": "86", "date_updated": "2009-06-04", "URL": ","Avatar ":"205659924 /dsc09920_normal.jpg ","follow_days ":"0 ","Start_followers ":"86 ","Growth_since ": 0,"평균_growth ":"0 ","내일 ":"86 ","86 ":"86 ":"86 ":"86 ":"86 ":"86 " ",", "86", "Rank": 176184, "followers_2w_ago": null, "growth_since_2w": 86, "평균_growth_2w": "6", "내일_2w": "92", "next_month_2w": "266 ","FollowersperDate ": []}
이 데이터는 URL에서 제공됩니다.
http://twittercounter.com/api/?username=anand_dasgupta&output=json&results=3(데이터를 얻으려면 URL을 클릭하십시오)
그러나 $ .ajax 함수의 data.json을 동일한 데이터를 포함하는 URL로 바꾸면 아래 코드는 작동하지 않는 것 같습니다 ...
$(document).ready(function() {
$('#content').html('');
$.ajax({
url:'http://twittercounter.com/api/?username=anand_dasgupta&output=json&results=3',
dataType: "json",
success: function(data) {
$('#content').append(''+data.rank+'
');
}
});});
나는 이전 에이 질문을 stackoverflow에 넣었고 응답은 이것이 크로스 도메인 문제라는 것이었다.
그래서 나는 Cross Domain Ajax 요청에 대해 읽었으며 이것이 제가 생각해 낸 코드입니다.
$(document).ready(function() {
$('form#search').bind("submit", function(e){
e.preventDefault();
$('#content').html('');
// Define the callback function
function get(jsonData) {
$('#content').append(''+jsondata.rank+'
');
bObj.removeScriptTag();
}
// The web service call
var req = 'http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3&callback=get';
// Create a new request object
bObj = new JSONscriptRequest(req);
// Build the dynamic script tag
bObj.buildScriptTag();
// Add the script tag to the page
bObj.addScriptTag();
});
});
그러나 심지어 작동하지 않는 것 같습니다.
누군가가 도움을 줄 수 있다면 깊이 감사 할 것입니다. 누구나 원하는 경우 스스로 테스트 할 수 있도록 전체 코드를 제공했습니다.
Anand에게 감사합니다