WordPress JQuery Ajax request readyState always 0

I can’t find an answer to this problem.

I’m trying to get the youtube video title (form WordPress of course, but I want to do it from javascript). Well my code looks like this:

Read More
$.ajax({
    url: "http://youtube.com/get_video_info",
    data: {video_id : v_arr[i]},
    type: 'GET',
    complete: function(jqXHR, res){
        if(jqXHR.readyState === 4) {
            alert(res);
        }
        alert(jqXHR.statusText);
    }
});

The problem: on ‘complete’ I always get readyState 0 and except for the “error” in statusText I don’t get any helpful error message.

Related posts

Leave a Reply

2 comments

  1. I tried running the ajax you provided and I got a 301 redirect response.

    Looking at the youtube api I see the url being https://gdata.youtube.com/feeds/api/videos/ which returns success

  2. I changed the url into what Circadian provided. The problem was also with i variable. As you see this is all happening inside a loop. So when the complete callback happened ‘i’ already had the last value from the loop… so when I accessed v_arr[i], there was the ‘undefined index’ problem.

    The solution is “temp i”:

    for(var i=0; i<v_arr.length; i++) {
        var temp_i = i;
        $.ajax({
            url: "https://gdata.youtube.com/feeds/api/videos/"+v_arr[temp_i],
            //data: {video_id : v_arr[temp_i]},
            type: 'GET',
            complete: function(jqXHR, res){
                if(jqXHR.readyState === 4) {
                    alert(res);
                }
                alert(jqXHR.statusText);
            }
        });
    }