criuiput
criuiput

Reputation: 11

how to use ajax response data in another javascript

I am working with google map.My map data come from php using ajax response.

My ajax code:

<script type="text/javascript">

    $.ajax({
    type: "POST",
    url: "mapajax.php",
    dataType:'text',
    success: function (result) {

                console.log(result); 

    }
    });
</script>

Now I have need to put my response data in my map var location

function initialize() {
    var locations = [
      //Now here I put my ajax response result
    ];

How can I do that?

Upvotes: 1

Views: 1855

Answers (4)

guest271314
guest271314

Reputation: 1

Is expected dataType response from $.ajax() to mapajax.php call text ?

Try

$(function () {
    function initialize(data) {
        var locations = [
        //Now here I put my ajax response result
        ];
        // "put my ajax response result"
        // utilizing `Array.prototype.push()`
        locations.push(data);
        // do stuff
        // with `locations` data, e.g., 
        return console.log(JSON.parse(locations));
    };
    $.ajax({
        type: "POST",
        url: "mapajax.php",
        dataType: 'text',
        success: function (result) {
            initialize(result);
        }
    });
});

jsfiddle http://jsfiddle.net/guest271314/maaxoy91/

See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

Upvotes: 0

Kevin Damstra
Kevin Damstra

Reputation: 125

This is done using callbacks, http://recurial.com/programming/understanding-callback-functions-in-javascript/ , here's a link if you want to read up on those. Let's see your current code here:

<script type="text/javascript">

$.ajax({
type: "POST",
url: "mapajax.php",
dataType:'text',
success: function (result) {
    console.log(result); 
}
});
</script>

As you noticed, the 'result' data is accessible in the success function. So how do you get transport it to another function? You used console.log(result) to print the data to your console. And without realizing it, you almost solved the problem yourself.

Just call the initialize function inside the success function of the ajax call:

<script type="text/javascript">

$.ajax({
type: "POST",
url: "mapajax.php",
dataType:'text',
success: function (result) {
    initialize(result); 
}
});

</script>

Upvotes: 0

Scott Mitchell
Scott Mitchell

Reputation: 698

Here is a fiddle with an example using $.when but its for SYNTAX only not making the call

http://jsfiddle.net/2y6689mu/

// Returns a deferred object
function mapData(){ return $.ajax({
    type: "POST",
    url: "mapajax.php",
    dataType:'text'
});
}
// This is the magic where it waits for the data to be resolved
$.when( mapData() ).then( initialize, errorHandler );

EDIT** function already returns a promise so you can just use

mapData().then()

per code-jaff comments

Upvotes: 1

Scimonster
Scimonster

Reputation: 33409

You'll have to refactor your code a little. I'm assuming you call initialize from the success callback.

Pass the locations array as an argument to initialize.

function initialize(locations) { ... }

$.ajax({
    type: "POST",
    url: "mapajax.php",
    dataType:'text',
    success: function (result) {
        initialize(result); 
    }
});

Then you can cut down even more and just do success: initialize, as long as initialize doesn't expect other parameters.

Upvotes: 1

Related Questions