funbinod 3 Posted April 14, 2018 Report Share Posted April 14, 2018 i was trying to use a ajax returned value out of the ajax function but it didn't work. can anyone please guide me. here is the code i tried. var lat, lng; $.ajax({ url: url, dataType: "json", method: "get", success: function(res) { var lat = res[imei].lat; var lng = res[imei].lng; } }); $("#lng").text(lng); $("#lat").text(lat); thanks in advance. Quote Link to post Share on other sites
Ingolme 1,020 Posted April 16, 2018 Report Share Posted April 16, 2018 Due to its asynchronous nature, the AJAX response has to be processed in the callback function. There isno way around that. Is there a reason why you can't move those lines of code inside the success function? Quote Link to post Share on other sites
funbinod 3 Posted April 16, 2018 Author Report Share Posted April 16, 2018 11 hours ago, Ingolme said: Is there a reason why you can't move those lines of code inside the success function? i don't know! i was just wondering for this. thank u for the reply. and i think i need to move those lines inside the success function. i wish some more guidance here. will u please help me?? from the ajax request, i get current latitude and longitude data from my gps tracker device. and to place a marker to the map i send those data to another page like below and load that page to a div in the current page. success: function(res) { var lat = res[imei].lat; var lng = res[imei].lng; if (loaded == "NO") { var loadmapurl = "gps.php?lat=" + lat + "&lng=" + lng; $("#loadmap").load(loadmapurl); loaded == "YES"; } else { var latlng = new google.maps.LatLng(lat, lng); marker.setPosition(latlng); } } setTimeout(executeQuery, 5000); this ajax request is loaded every 5000ms. for each ajax success function, i check if the map page is loaded already or not. and if the page is loaded already loaded i wish not to load the page anymore but to move the marker according the current latitude and longitude value. the marker variable inside the else block is from the loaded page (i.e.: "gps.php?........"). is the above try good to perform my requirement? is the above try to load a map is good enough? or i've to try something else to load a map? (NOTE: i cannot say what happens with this current code because there is a header setting error (i.e.: access-control-allow-origin header not present......) on my gps service provider api link.) thank you in advance. Quote Link to post Share on other sites
justsomeguy 1,135 Posted April 16, 2018 Report Share Posted April 16, 2018 If marker isn't defined on that same page, it's not going to be defined in that function for you to update. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.