AJAX/JS noob here. I have the following AJAX code that returns some data from a php service. I am trying to find out how to assign the data that returns from the php service to some JS variable which I later want to display on the UI.
Here is my AJAX code:
<script type="text/javascript">
var data1;
$(function(){
setInterval(function() {
$.get("get_location.php", function(data) {
console.log(data);
$('#data').html(data);
data1 = data;
});
}, 500);
});
alert(data1);
</script>
And I have a variable in another script instance:
<script>
var some_var1;
var some_var2;
</script>
I have two questions that I have been stuck on for a while:
The way my PHP returns the data is though echo, meaning I do this in PHP:
echo $var1; echo $var2;
Is this the right way to pass the data to AJAX given that I need it more structured?
- Assuming I did #1 correctly, how do I assign "data" from AJAX to some_var1/some_var2 in JS?