1

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?

1
  • You can have a variable outside to set it to but this is also bad practice since this is an AJAX request you dont know when this will complete. Since you are using jquery I'd look into $.when Commented Aug 10, 2014 at 15:16

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly fine but I prefer to use $.when in these situations as opposed to the success callback. I guess its just a preference thing?
1

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

2 Comments

you don't need to wrap that function with when, simply mapData().then() would be enough, since ajax already returns the promise.
Thanks you are correct! I did not realize you could do that actually
0

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.