I've been trying to load an api key from a local file, and I have the following code:
<script>
jQuery.get("key.txt", undefined, function(response)
{
src="https://maps.googleapis.com/maps/api/js?key=" + response + "&libraries=places";
});
</script>
However, when it comes to the google maps api actually being accessed, the following error is thrown:
VM1464:1 Uncaught SyntaxError: Unexpected token C in JSON at position 0(…)
which is thrown in some function in jquery. I am not sure whether this error comes from the fact that src is never set at all, or whether I am just not dealing with asynchronicity properly. Any help is greatly appreciated, thanks.
EDIT
Having updated the code:
$.get("key.txt", function(response)
{
src="https://maps.googleapis.com/maps/api/js?key=" + response + "&libraries=places";
console.log(src);
});
src appears in the console exactly as it should. However, after doing various things on the webpage, and then clicking on a button - which calls a function that accesses a php page utilizing the google maps api:
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=".$userLat.",".$userLng."&destinations=".$helperLat.",".$helperLng."&Key=".$key;
//can specify mode (default driving), language etc.
try{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CAINFO, "crt.PEM");
$auth = curl_exec($curl);
...
where $userLat, $userLng, $helperLat, $helperLng and $key are (successfully) initialised previously - the error is thrown.
EDIT
I guess what I am really asking is, after dealing with asychronicity, how do I put the return value into the src attribute of an html script tag. I have this so far, but it does not work:
<script src="" onload="getSource(function(result){ return result; });">
function getSource(callback)
{
$.get("key.txt", function(response)
{
callback("https://maps.googleapis.com/maps/api/js?key=" + response + "&libraries=places");
});
}
</script>