2010-08-28 53 views
2

我目前正在使用ajax trough jquery庫來處理google的apis。 爲了使用谷歌服務API,我需要獲得身份驗證令牌發送請求到ClientLogin。其實,我不知道如何將令牌傳遞給第二個請求。將ajax響應設置爲全局變量

我給自己定了一個全局變量令牌var token = null;

我呼籲$(document).ready event兩個請求。

  1. 第一個是一個HTTPS POST請求 谷歌的ClientLogin在 爲了獲得用戶憑證令牌。

這裏的第一個AJAX請求的代碼:

$.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 
  • 第二個應該調用與HTTP GET觸點API。

    $.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>title').text(); 
        $(".status").text(names); 
    }, 
    error: function(xhr, status, error) { 
        $(".status").html(xhr.status+ " "+ xhr.statusText); 
    } 
    

    }); //結束GOOGLE聯繫請求

  • 我面臨的問題是當我嘗試在第二個請求中設置Google鑑定標題時,標記設置爲空。我已經閱讀link text,但這不適合我。我現在必須與回調/事件有關,但我無法想象如何做到這一點。

    任何幫助表示讚賞

    回答

    3

    問題進行了更新:

    你重新聲明在命名token第一個請求一個局部變量,因此它不使用你null發起的全球token

    刪除第一個請求中的var關鍵字。

    token = tokenArray[3]; 
    

    如果要求按順序運行,那麼第二個將不等待第一它執行之前接收其響應。

    如果是這種情況,請將第二個請求置於function中,並從第一個success:回調中調用該函數。

    $.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); 
         // Call second request here 
         secondRequest(token); // optionally pass the "token" 
               // instead of using global var 
        } 
    }); // END OF CLIENT LOGIN REQUEST 
    
    
    function secondRequest(token) { 
        $.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>title').text(); 
         $(".status").text(names); 
        }, 
        error: function(xhr, status, error) { 
         $(".status").html(xhr.status+ " "+ xhr.statusText); 
        } 
        }) 
    } 
    

    (在這段代碼中,你實際上可以擺脫全球token變量的,只是從第一個請求到第二作爲函數參數傳遞token

    +0

    非常感謝帕特里克你的答案。 我更新了代碼,但當我打電話給第二個請求時,腳本崩潰了。 當我將授權標頭設置爲標記值時,它會中斷。我有以下控制檯錯誤: 錯誤:未捕獲的異常:[異常...「組件返回失敗代碼:0x80070057(NS_ERROR_ILLEGAL_VALUE)[nsIXMLHttpRequest.setRequestHeader]」nsresult:「0x80070057(NS_ERROR_ILLEGAL_VALUE)」位置:「JS框架: :GContactsAPI.js :: anonymous :: line 45「data:no] 任何想法? – anchnk 2010-08-28 14:36:57

    +0

    @anchnk - 你是否驗證'token'在'secondRequest()'裏面看起來正確?我不知道你從第一次請求中得到的迴應,或者如何正確設置第二次請求。我會在'secondRequest()'函數中使用console.log(token)'來確保它具有您期望的值。 – user113716 2010-08-28 14:43:24

    +0

    是的,我得到的價值與完整回覆相同。我會嘗試重新啓動瀏覽器並查看。另一點是,我從現在開始從本地文件執行該請求。所以我添加了netscape.security.PrivilegeManager.enablePrivilege(「UniversalBrowserRead」);能夠發送請求,但我不確定它與此有關。 – anchnk 2010-08-28 14:56:52

    0

    看起來像變量標記的範圍不是全局的。你有它在成功函數的範圍內定義的初始ajax請求

    +0

    對不起,這是複製/粘貼過程中的錯誤,我編輯帖子以反映當前版本的源代碼,它沒有var關鍵字不工作。 – anchnk 2010-08-28 14:13:18