Note that there are some explanatory texts on larger screens.

plurals
  1. POset ajax response as global variable
    primarykey
    data
    text
    <p>I'm currently messing with google's apis using ajax trough jquery library. In order to use a google service API i need to get an auth token sending a request to ClientLogin. Actually, i've no idea how to pass the token to the second request .</p> <p>I've set a global variable token as <code>var token = null;</code></p> <p>I call two requests on <code>$(document).ready event</code>.</p> <ol> <li>The first one is an https POST request to google clientLogin in order to get user credential token. </li> </ol> <p>Here's the code of the first ajax request :</p> <pre><code>$.ajax({ type: "POST", url: "https://" + host + clientLoginEntryPoint, data: cLRequestData(accountType, user, pwd, service), dataType: "html", success: function (response) { var tokenArray = response.split("="); // Split to response tokenArray[3] is the auth token token = tokenArray[3]; $(".status").html(token); } }); // END OF CLIENT LOGIN REQUEST </code></pre> <ol start="2"> <li><p>The second one is supposed to call the contacts API with an http GET.</p> <pre><code>$.ajax({ type: "GET", url: "http://" + host + googleContactEntryPoint, beforeSend: function(xhr) { xhr.setRequestHeader('Authorization', 'GoogleLogin auth=' + token); xhr.setRequestHeader('GData-Version', '3.0'); }, success: function(response, textStatus, xhr) { var names = $(response).find('entry&gt;title').text(); $(".status").text(names); }, error: function(xhr, status, error) { $(".status").html(xhr.status+ "&amp;nbsp;"+ xhr.statusText ); } </code></pre> <p>}); // END OF GOOGLE CONTACT REQUEST</p></li> </ol> <p>The problem i'm facing is that token is set to null when i try to set Google Authentification Header in the second request. I've already read <a href="https://stackoverflow.com/questions/905298/jquery-storing-ajax-response-into-global-variab">link text</a> but this is not working for me. I now that must be something to do with callback/events but i'm couldn't figure how to do this.</p> <p>Any help appreciated</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload