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

You need to declare all_skills before the ajax call.

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

Member Avatar for diafol

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

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
:(

Member Avatar for diafol

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/

var jsarray = jQuery.parseJSON(data);

commented: Yup! +32

Ah, Rough is correct!

Just doing all_skills=data will just assign a JSON string to all_skills. If he wants it to actually be a native array, you have to use ParseJSON.

I know this is a two year old threat, but better late than never, for others who may stumble upon this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.