[RESOLVED]Issue to get data on time

  //function return video array which get from web service
	var getVideoArrayByService = function (vid)
	{
	    var vArray= [];
	    $.getJSON("http://localhost:2364/api/advertapi/"+vid).done(function (data) {


           
	        var AllAdvert = data;
	             
	         vArray[0] = AllAdvert[0].Advertisement.AdvertisementURL;
	         vArray[1] = AllAdvert[0].Video.VideoURL;
	         vArray[2] = AllAdvert[1].Advertisement.AdvertisementURL;
	        
	       	       

       });

	   
	    return vArray;

	};

Above code get 2 advertisement  and 1 video url from web API

   public class AdvertAPIController : ApiController
    {
        VideoEntities db = new VideoEntities();

        [HttpGet]
        // GET api/advertapi/5
        public IEnumerable<AdvertisementRequest> Get(int id)
        {
            //do some randomization and return 1 Advertisment or MAXIMUM 2 records means two advertisemnt
            var advertisements = db.AdvertisementRequests.Where(a => a.VideoId == id).ToList();
            return advertisements;
        }

    }

Here on var tArray = getVideoArrayByService(video_id); if i wait some time then everything works fine, else it return undefined

if (video_id != null)
	{
	    var tArray = getVideoArrayByService(video_id);


	    videoArray = [tArray[0].toString(),tArray[1].toString(),tArray[2].toString()];
	}
	else
	{
	    alert("No video id provided so loading default videos");
        videoArray = ["http://localhost:2364/Videos/1.m4v"];
	  
	}

 

Development in: ASP.Net MVC Version 4

Issue is:

jquery call $.getJSON(..) get data from API late, i think its async because of this i get undefined array.

If i debug it in firefox and wait for a while, than every thing working fine.

Can any body explain how to solve this issue?

either use sync on getJson, either make array processing in the .done

i get an idea from

http://stackoverflow.com/questions/133310/how-can-i-get-jquery-to-perform-a-synchronous-rather-than-asynchronous-ajax-re?rq=1

Solution what i have found is:

var getVideoArrayByService = function (vid)
	{
	    jQuery.ajaxSetup({ async: false });

	    var vArray= [];
	    $.getJSON("http://localhost:2364/api/advertapi/"+vid).done(function (data) {


            //get only two filetered advertisements here only 2
	        var AllAdvert = data;
	     
	        if (AllAdvert.length == 1)
	        {
                ////Case when video has no advertisements associated
	            //if (AllAdvert[0].AdvertisementRequestId == -1)
	            //{
	            //    vArray[0] = AllAdvert[0].Video.VideoURL;
	            //}
	            //else
	            //{
	                alert(AllAdvert[0].Advertisement.AdvertisementURL + " VideoURL ->" + AllAdvert[0].Video.VideoURL);
	                vArray[0] = AllAdvert[0].Advertisement.AdvertisementURL;
	                vArray[1] = AllAdvert[0].Video.VideoURL;
	            //}
	        }
	        else if (AllAdvert.length == 2)
	        {
	            vArray[0] = AllAdvert[0].Advertisement.AdvertisementURL;
	            vArray[1] = AllAdvert[0].Video.VideoURL;
	            vArray[2] = AllAdvert[1].Advertisement.AdvertisementURL;
	        }
	        //for (var i = 0; i < AllAdvert.length; i++)
	        //{
	        //    vArray[vArray.length] = AllAdvert[i].Advertisement.AdvertisementURL;
	            
	        //}
	       
	        jQuery.ajaxSetup({ async: true });
       });

Thank you for your time and support.

Leave a Reply