0

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>
w1nter
  • 337
  • 4
  • 23
  • 1
    What is the actual value of `response`? – Dan Wilson Nov 21 '16 at 18:51
  • What's the output of `console.log(response)`. – Zakaria Acharki Nov 21 '16 at 18:53
  • There is nothing really "dealing" with asynchronisity here. You make an async request and then set a variable when it comes back. Do you _try_ to use the variable afterwards? If so, how? And where is that error coming from - is it from this call or a different one? – VLAZ Nov 21 '16 at 18:53
  • 1
    Why are you using `undefined`? get rid of that. – epascarello Nov 21 '16 at 18:55
  • correct syntax `$.get( "yourfile", function( data ) { /*do something*/ });` – Aniket Sahrawat Nov 21 '16 at 18:56
  • response is just the key, which is stored in the text file. logging src gives the response i'd expect – w1nter Nov 21 '16 at 18:57
  • So how are you using src? My guess is you are using it before the value is set. – epascarello Nov 21 '16 at 18:57
  • So how is it actually used.... What uses `src`? – epascarello Nov 21 '16 at 19:02
  • I was under the impression that it takes a while to call the jquery function, and therefore if it is used before it is properly modified, unexpected behaviour happens. However, in this case, by the time I use src, it would have been set appropriately, surely? @epascarello – w1nter Nov 21 '16 at 19:13

0 Answers0