admin管理员组

文章数量:1026989

How to use data outside of function returned from POST or GET method of jquery?

My question is i'm getting some data through jquery post method.. the problem is whenever i get data i'm just able to use that within function actually i want that data to use outside of function.. as i alerted data outside function it's not working...

function getlatlanfromjs() {
    $(document).ready(function() {
        var uid = $(".track_textbox").val();

        $.post("getLatLan.php", {userid: uid}, function(data) {
            var i;
            for (i = 1; i < 2; i++) {                
                initialize(data[i]["latitude"], data[i]["longitude"]);
            }
        }, "json");

        alert(data[1]["latitude"]); //this is actually not alerting..  what is the reason ?
    });
}

How to use data outside of function returned from POST or GET method of jquery?

My question is i'm getting some data through jquery post method.. the problem is whenever i get data i'm just able to use that within function actually i want that data to use outside of function.. as i alerted data outside function it's not working...

function getlatlanfromjs() {
    $(document).ready(function() {
        var uid = $(".track_textbox").val();

        $.post("getLatLan.php", {userid: uid}, function(data) {
            var i;
            for (i = 1; i < 2; i++) {                
                initialize(data[i]["latitude"], data[i]["longitude"]);
            }
        }, "json");

        alert(data[1]["latitude"]); //this is actually not alerting..  what is the reason ?
    });
}
Share Improve this question edited Apr 14, 2014 at 20:22 honk 9,75311 gold badges80 silver badges87 bronze badges asked Apr 14, 2014 at 20:19 Sha BeerSha Beer 273 silver badges9 bronze badges 5
  • 2 It's probably calling alert() before the post request returns, that's AJAX for you. – xd6_ Commented Apr 14, 2014 at 20:21
  • If you want to alert outside of the callback, you need to use a promise. Look into jQuery deferred to see how it works. – Trace Commented Apr 14, 2014 at 20:23
  • That's exactly what it's doing @xd6_, it's an Asynchronous request. Why not just continue your code from within the function with the response data? Call a function from there and continue your code. – Adam Commented Apr 14, 2014 at 20:24
  • take a look at the last example in https://api.jquery./jQuery.post/, I think the .done method is what you are looking for – Joe Commented Apr 14, 2014 at 20:25
  • @Joe - the function(data) argument to $.post is just that, actually. – xd6_ Commented Apr 14, 2014 at 20:26
Add a ment  | 

3 Answers 3

Reset to default 2

Because data variable is out of scope at that point. Code below will assign the data to the local recievedData and you can use it out of the handling function scope. Though you will still be only able to access recievedData variable only when request is done.

$(document).ready(function() {

    var recievedData;
    var uid = $(".track_textbox").val();


    var promise = $.post("getLatLan.php", {userid: uid}, function(data) {
        recievedData = data;
        var i;
        for (i = 1; i < 2; i++) {

            initialize(data[i]["latitude"], data[i]["longitude"]);

        }



    }, "json");

   promise.done(function() {
    alert(recievedData[1]["latitude"]); });

});

this is a bit better...

This way you dont miss the execution. If you wrap your code into a closure, you can call it later too. So even though your "getlatlanfromjs" has already executed, your alertFn still exists and will be called when the $.post is done.

function getlatlanfromjs() { $(document).ready(function() { var uid = $(".track_textbox").val();

    var alertFn = function(data) { alert(data[1]["latitude"]); };

    $.post("getLatLan.php", {userid: uid}, function(data) {
        var i;
        for (i = 1; i < 2; i++) {                
            initialize(data[i]["latitude"], data[i]["longitude"]);
        }
        alertFn(data);
    }, "json");

}); }
   var mydata = null;

    function getlatlanfromjs() {
    $(document).ready(function() {
        var uid = $(".track_textbox").val();

        $.post("getLatLan.php", {userid: uid}, function(data) {
            mydata = data;
            var i;
            for (i = 1; i < 2; i++) {                
                initialize(data[i]["latitude"], data[i]["longitude"]);
            }
        }, "json");
        if(mydata != null)   alert(data[1]["latitude"]); 
    });
}

All you needed to do was keep track of the scope of data.

How to use data outside of function returned from POST or GET method of jquery?

My question is i'm getting some data through jquery post method.. the problem is whenever i get data i'm just able to use that within function actually i want that data to use outside of function.. as i alerted data outside function it's not working...

function getlatlanfromjs() {
    $(document).ready(function() {
        var uid = $(".track_textbox").val();

        $.post("getLatLan.php", {userid: uid}, function(data) {
            var i;
            for (i = 1; i < 2; i++) {                
                initialize(data[i]["latitude"], data[i]["longitude"]);
            }
        }, "json");

        alert(data[1]["latitude"]); //this is actually not alerting..  what is the reason ?
    });
}

How to use data outside of function returned from POST or GET method of jquery?

My question is i'm getting some data through jquery post method.. the problem is whenever i get data i'm just able to use that within function actually i want that data to use outside of function.. as i alerted data outside function it's not working...

function getlatlanfromjs() {
    $(document).ready(function() {
        var uid = $(".track_textbox").val();

        $.post("getLatLan.php", {userid: uid}, function(data) {
            var i;
            for (i = 1; i < 2; i++) {                
                initialize(data[i]["latitude"], data[i]["longitude"]);
            }
        }, "json");

        alert(data[1]["latitude"]); //this is actually not alerting..  what is the reason ?
    });
}
Share Improve this question edited Apr 14, 2014 at 20:22 honk 9,75311 gold badges80 silver badges87 bronze badges asked Apr 14, 2014 at 20:19 Sha BeerSha Beer 273 silver badges9 bronze badges 5
  • 2 It's probably calling alert() before the post request returns, that's AJAX for you. – xd6_ Commented Apr 14, 2014 at 20:21
  • If you want to alert outside of the callback, you need to use a promise. Look into jQuery deferred to see how it works. – Trace Commented Apr 14, 2014 at 20:23
  • That's exactly what it's doing @xd6_, it's an Asynchronous request. Why not just continue your code from within the function with the response data? Call a function from there and continue your code. – Adam Commented Apr 14, 2014 at 20:24
  • take a look at the last example in https://api.jquery./jQuery.post/, I think the .done method is what you are looking for – Joe Commented Apr 14, 2014 at 20:25
  • @Joe - the function(data) argument to $.post is just that, actually. – xd6_ Commented Apr 14, 2014 at 20:26
Add a ment  | 

3 Answers 3

Reset to default 2

Because data variable is out of scope at that point. Code below will assign the data to the local recievedData and you can use it out of the handling function scope. Though you will still be only able to access recievedData variable only when request is done.

$(document).ready(function() {

    var recievedData;
    var uid = $(".track_textbox").val();


    var promise = $.post("getLatLan.php", {userid: uid}, function(data) {
        recievedData = data;
        var i;
        for (i = 1; i < 2; i++) {

            initialize(data[i]["latitude"], data[i]["longitude"]);

        }



    }, "json");

   promise.done(function() {
    alert(recievedData[1]["latitude"]); });

});

this is a bit better...

This way you dont miss the execution. If you wrap your code into a closure, you can call it later too. So even though your "getlatlanfromjs" has already executed, your alertFn still exists and will be called when the $.post is done.

function getlatlanfromjs() { $(document).ready(function() { var uid = $(".track_textbox").val();

    var alertFn = function(data) { alert(data[1]["latitude"]); };

    $.post("getLatLan.php", {userid: uid}, function(data) {
        var i;
        for (i = 1; i < 2; i++) {                
            initialize(data[i]["latitude"], data[i]["longitude"]);
        }
        alertFn(data);
    }, "json");

}); }
   var mydata = null;

    function getlatlanfromjs() {
    $(document).ready(function() {
        var uid = $(".track_textbox").val();

        $.post("getLatLan.php", {userid: uid}, function(data) {
            mydata = data;
            var i;
            for (i = 1; i < 2; i++) {                
                initialize(data[i]["latitude"], data[i]["longitude"]);
            }
        }, "json");
        if(mydata != null)   alert(data[1]["latitude"]); 
    });
}

All you needed to do was keep track of the scope of data.

本文标签: javascriptHow to use data outside of function returned from POST or GET method of jqueryStack Overflow