funbinod Posted April 14, 2018 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. Link to comment Share on other sites More sharing options...
Ingolme Posted April 16, 2018 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? Link to comment Share on other sites More sharing options...
funbinod Posted April 16, 2018 Author 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. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 16, 2018 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now