0

Hello,

Sorry,This may be a silly question :

How to assign ajax return value to javascript array?
for example what I want is,
I have declared an array in javascript as

all_skills = new Array();

and after that called ajax call in which I want to save the return value of ajax (on success) to this declred array.

$.ajax({
                url:'index.php?r=Skill_form/FetchAllSkills',
                type:'POST',
                data:name={},
                success:function(data){
                                            alert(data);
                                      }
            });

How to do this?

alert(data) gives me result as : ["test db skill 3","vv"]
i want to assign this array to all_skills

Edited by Priti_P

3
Contributors
5
Replies
14
Views
3 Years
Discussion Span
Last Post by diafol
0

Thanks for reply!
I tried

        t =$.ajax({
                url:'index.php?r=Skill_form/FetchAllSkills',
                type:'POST',
                data:name={},
                success:function(data){
                                            all_skills = [];
                                            all_skills=data;
                                      }
                });

        alert(all_skills + " is skill array"); // which gives me error as all_skils is not defined

as well as

    all_skills = [];
        t =$.ajax({
                url:'index.php?r=Skill_form/FetchAllSkills',
                type:'POST',
                data:name={},
                success:function(data){
                                            all_skills=data;
                                      }
                });

        alert(all_skills + " is skill array"); // WHICH GIVES EMPTY VALUE 

Please tell me where am wrong here

0

Place the alert inside the success bit:

success: function(data){
    all_skills=data;
    alert(all_skills + " is skill array"); 
}

However jQuery suggest that you use the newer .done instead of success

0

it is not about alerting the array,
it is about how to assign the same array outside ajax call.
when I write

 success:function(data){
all_skills=data;
return data
}

and alert t,
then it says me object object
I just want an array outside
:(

0

You can't - not really, as the ajax call is asynchronous. You can write the array to a document scope variable, but there's no guarantee that that will happen before some other javascript relying on this 'new' data needs it. Ideally, you'd run your functions from within the success function.

You could set the asynchronous (async) parameter to false, but this is not recommended.

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 with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().

From http://api.jquery.com/jquery.ajax/

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.