YouTube 동영상을 표시하는 애플리케이션이 있으며 사용자가 동영상을 좋아하거나 좋아할 수있는 속도 버튼이 있습니다. click 이벤트에서 3 개의 함수는 ajax의 success
함수를 통해 함께 연결됩니다. ytvRate()
->getRating()
->YouTube Data API 3 속도 엔드 포인트에 시간차가 있습니까?
내가 조치를 기록하고 내가 ytvRate()
에서 보낸 값이없는 getRating()
의 응답 결과 showRating()
: 흐름이있다. 잠시 기다렸다가 페이지를 새로 고치면 getRating()
의 결과가 올바르게 반환됩니다. 나는 getRating()
을 아약스의 성공 함수 안에서 ytvRate()
이라고 부른다. 성공 응답을받을 때까지는 함수를 호출해서는 안된다는 뜻입니까? 여기
rating sent: like
call get rating
this is my rating: none
call show rating
이 등급은 API에서 반환 정확하지 않습니다 - 그것은 그냥 보낸 평가해야한다. 새로 고침시 동일한 호출이 올바른 등급을 반환합니다 ... 그렇다면 올바른 정보를 업데이트하는 데이터 API에 지연이나 무언가가 있습니까? 어떻게하면 요청을 보내는 버튼 클릭으로 동일한 등급을 얻을 수 있습니까? 여기
은 (showRating
이 문제에 관련하지 않는 것 그것은 한 올바른 평가수록 잘 작동 - 그렇지..) 기능이다
function ytvRate(id, rating, event){
event.preventDefault()
var apiKey = 'This is a valid key';
var client_id = 'This is a valid client id';
var redirect_uri = 'This is a redirect uri';
var scope = 'https://www.googleapis.com/auth/youtube';
var rateUrl = 'https://www.googleapis.com/youtube/v3/videos/rate?id='+id+'&key='+apiKey+'&rating='+rating;
if(getHash().access_token){
var token = getHash().access_token;
$.ajax({
type: "POST",
url: rateUrl,
beforeSend: function (request){
request.setRequestHeader('Authorization', 'Bearer ' + token);
},
success: function(data){
console.log('rating sent: '+rating);
getRating(id);
},
error: function(e) {
console.log(e);
}
});
} else{
window.location = 'https://accounts.google.com/o/oauth2/v2/auth?client_id='+client_id+'&redirect_uri='+redirect_uri+'&scope='+scope+'&response_type=token&prompt=consent&include_granted_scopes=false';
}
return false;
}
function getRating(id){
var getRatingUrl = 'https://www.googleapis.com/youtube/v3/videos/getRating?id='+id;
console.log('call get rating');
if(getHash().access_token){
var token = getHash().access_token;
$.ajax({
type: "GET",
url: getRatingUrl,
beforeSend: function (request){
request.setRequestHeader('Authorization', 'Bearer ' + token);
},
success: function(data){
var rating = data.items[0].rating;
console.log("this is my rating: " + rating);
showRating(rating, id);
}
});
}
}
function showRating(response, id){
console.log('call show rating');
numLikes(id);
if(response == 'like'){
document.getElementById("notliked").className = "hide";
document.getElementById("liked").className = "";
document.getElementById("like-btn").style.color = "#87CEFA";
document.getElementById("like-btn").setAttribute("onclick", "ytvRate('"+id+"', 'none', event)");
} else{
document.getElementById("notliked").className = "";
document.getElementById("liked").className = "hide";
document.getElementById("like-btn").style.color = "inherit";
document.getElementById("like-btn").setAttribute("onclick", "ytvRate('"+id+"', 'like', event)");
}
}
편집 :
흥미롭게도 대신 새로운 방법으로 youtube/v3/videos
리소스를 호출하고 statistics.likeCount
에 액세스하면 즉시 번호가 업데이트됩니다. 동일한 효율성으로 사용자 등급을받을 수없는 이유는 무엇입니까?
'getRating' 요청에서 캐싱을 사용하지 않으려 했습니까? '$ .ajax ({cache : false, ...}. 이전에 요청한 경우 응답이 캐시 될 수 있습니다. 그렇지 않으면 스팸 투표를 방지하기 위해 응답을 즉시 업데이트하지 않을 수도 있습니다. –
@MikeC Nice 제안 ... 방금 시도한 결과가 동일합니다. 행운 없음 – mhatch
GET 대신 POST 요청을 보낼 수 있습니까? POST 요청을 캐시해서는 안됩니다. 또 다른 옵션은 고유 한 무작위 캐시 유효성 검사 문자열을 & nocache = RANDOMSTRING과 같은 url을 요청하십시오. 모두 실패하면 googleapis 오류 일 수 있습니다. –