Undefined value returned by Ajax!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hatem Salem
    New Member
    • May 2012
    • 7

    Undefined value returned by Ajax!

    I am trying to get all Following IDs using Twitter API. Now on Load function am trying to alert the IDs array (FollowingArray ) which returned by GetFollowing.

    I've test the value of array inside the function , but it looks OK. When i try to alert it appears as undefined.

    Code:
    function Load()
    {
    	alert(GetFollowing("hatem"));
    }
    
    function GetFollowing(ScreenName)
    {
    	var Following = $.ajax (
    		
    		{
    			url: "https://api.twitter.com/1/following/ids.json?cursor=-1&screen_name=" + ScreenName,
    			dataType:"jsonp",
    			timeout:10000,
    			async:false
    		}
    		
    	);
    	
    
    	var FollowingArray = new Array();
    	
    	Following.success
    	(
    		function(Follow)
    		{
    			i = 0;
    			while(Follow["ids"][i])
    			{
    				FollowingArray[i] = JSON.stringify(Follow["ids"][i]);
    				i++;
    			}
    		}	
    	);
    	
    	Following.fail
    	(
    		function()
    		{
    			alert("Error!")
    		}
    	);
    	
    	Following.complete
    	(
    		function()
    		{	
    			return FollowingArray;
    		}
    	);
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I think it doesn’t work because of the SOP (Same Origin Policy).

    Comment

    • Hatem Salem
      New Member
      • May 2012
      • 7

      #3
      The value look OK when alerting it inside the function. But when we try to get by return fails !

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        1) jqXHR.success() is deprecated
        2) jqXHR.complete( ) is an event, returning from an Event does almost nothing
        3) synchronous AJAX in jQuery note:
        By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false is deprecated.

        Comment

        • Hatem Salem
          New Member
          • May 2012
          • 7

          #5
          Then, What is the solution ? !

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            use FollowingArray. it should already contain the needed values.

            Comment

            • Hatem Salem
              New Member
              • May 2012
              • 7

              #7
              I will make local service using PHP to get the needed data using normal ajax request. I think this may helps !

              Comment

              Working...