2013-04-09 25 views

回答

13

您有權請求應答頭這樣的:

chrome.webRequest.onHeadersReceived.addListener(function(details){ 
    console.log(details.responseHeaders); 
}, 
{urls: ["http://*/*"]},["responseHeaders"]); 

使用的一個例子。這是我在我的擴展中如何使用webRequest api的一個例子。 (僅顯示部分不完整的代碼)

我需要間接訪問某些服務器數據,我通過使用302重定向頁面來完成此操作。我送一個Head請求所需的URL是這樣的:

$.ajax({ 
    url: url, 
    type: "HEAD" 
    success: function(data,status,jqXHR){ 
    //If this was not a HEAD request, `data` would contain the response 
    //But in my case all I need are the headers so `data` is empty 
    comparePosts(jqXHR.getResponseHeader('redirUrl')); //where I handle the data 
    }  
}); 

然後我默默地殺重定向而刮使用webRequest API我自己的用途location頭:

chrome.webRequest.onHeadersReceived.addListener(function(details){ 
    if(details.method == "HEAD"){ 
    var redirUrl; 
    details.responseHeaders.forEach(function(v,i,a){ 
     if(v.name == "Location"){ 
     redirUrl = v.value; 
     details.responseHeaders.splice(i,1); 
     } 
    }); 
    details.responseHeaders.push({name:"redirUrl",value:redirUrl}); 
    return {responseHeaders:details.responseHeaders}; //I kill the redirect 
    } 
}, 
{urls: ["http://*/*"]},["responseHeaders","blocking"]); 

我真正處理onHeadersReceived監聽器中的數據,但這種方式顯示響應數據的位置。

+0

謝謝。我知道了。我錯過了'[「responseHeaders」]'。反正,最後一件事。如何查看像XML/HTML或JSON響應內容的實際響應? – madziikoy 2013-04-09 02:05:13

+0

@madziikoy你將不得不用你的實際請求邏輯來處理。 webRequest API只處理有關請求本身的信息。 – BeardFist 2013-04-09 02:08:04

+0

對不起,我沒有得到它我與鉻擴展API新。 :) – madziikoy 2013-04-09 02:10:58