Jump to content

return ajax data outside the ajax


funbinod

Recommended Posts

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...