Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:29:27+00:00 2026-06-07T17:29:27+00:00

Currently fetching the first video from a search on YouTube using the code below,

  • 0

Currently fetching the first video from a search on YouTube using the code below, but when docready is called, the ids are not set. The code does not wait for the ajax results to be returned, instead it continues and returns nothing.

I’ve tried the async:false with the ajax thinking it might make it ‘wait’ but still nothing is returned. How would I fix this so all the ids will be set?

$(document).ready(function () {
    docready();
});

function docready() {
    var id1 = grabid("cat");
    var id2 = grabid("dog");
    var id3 = grabid("goldfish");
    alert(id1);
}

function grabid(keyword) {
    var url = 'http://gdata.youtube.com/feeds/api/videos?q=' + encodeURIComponent(keyword) + '&format=5&max-results=1&v=2&alt=jsonc';
    $.ajax({
        async: false,
        type: "GET",
        url: url,
        dataType: "jsonp",
        success: function (responseData, textStatus, XMLHttpRequest) {
            if (responseData.data.items) {
                var videos = responseData.data.items;
                videoid = videos[0].id;
                alert(videoid);
                return videoid;
            }
        }
    });
}

UPDATE: I’ve just changed the code above a bit to make my problem a bit clearer. When the code is run it alert’s the real ‘videoid’ 3 times as expected. But when it gets to alert(id1) it returns undefined – so the ‘return videoid’ is ignored and not passing it back to the variable.

UPDATE 2 STILL UNSOLVED

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:29:29+00:00Added an answer on June 7, 2026 at 5:29 pm

    Firstly you have problem with your ajax call try this..

     $.ajax({
         type: "GET",
         url: url,
         dataType: "jsonp",
         success: function (responseData) {
                      var videos = responseData.data.items;
                      var videoid=videos[0].id;
                       alert(videoid);
                      //pass this videoid to anyother JS function it should work
                    },
         error: function (xhr, textStatus, XMLHttpRequest) {
                     alert(xhr.responseText);
                    }   
    
     });
    

    EDIT:

    Updated code to get return from ajax call :

     function grabid(keyword){
       var url = 'http://gdata.youtube.com/feeds/api/videos?q='+encodeURIComponent(keyword)+'&format=5&max-results=1&v=2&alt=jsonc';
      return $.ajax({
         type: "GET",
         url: url,
         dataType: "jsonp"       
        });   
    
    }
    
    
    
    function getVideoId(key) {
      key.success(function(respdata) {
          var videos = respdata.data.items;
          var videoid=videos[0].id;
         alert(videoid)
       });
      }
    
    var ids = grabid(123);
    getVideoId(ids);
    

    Remove all your onload or document ready functions,this should work.

    EDIT :

    The only thing you can do is doing async:false, which will wait for the ajax call to finish and return a value… but CROSS DOMAIN calls are not synchronous so I can say you are out of luck.. You need to re think on the logic of ajax calls.

    Also As of jQuery 1.8, the use of async: false is deprecated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently fetching data from a MySQL database using JDBC and executeQuery . One
I am currently developing a Facebook App (it's not my first one) and I
I am writing some code to update an application by fetching new dlls from
I'm currently fetching an array with .each: $.each(messages, function(key,message){ doStuff(); }); But the key
I am currently asynchronously fetching json documents to update elements in a few html
I am fetching the current date & time using NOW() in mysql. I want
Currently I am using HTML files for parts of my user interface. I display
currently, I`m implementing a map App with Mono4Droid and there I`m using a WebView
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);
Hello I have a problem with fetching required rows from MySQL table correctly within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.