PhantomJS - onResourceReceived()



當頁面請求的資源被接收時呼叫此回撥。它包含響應作為引數。

響應物件具有以下詳細資訊 -

  • Id - 請求的資源編號。

  • URL - 請求的 URL。

  • 時間 - 包含響應日期的日期物件。

  • 頁首 - http 頁首。

  • BodySize - 解壓縮的接收內容的大小。

  • ContentType - 指定的內容型別(如果已指定)。

  • RedirectURL - 如果存在重定向,則重定向 URL。

  • 階段 - 值為 - 開始和結束。

  • 狀態 - http 程式碼狀態,即狀態 200。

  • StatusText - 程式碼 200 的 http 狀態文字,它是 OK。

語法

其語法如下 -

page.onResourceReceived = function(response) {}

示例

以下程式碼顯示瞭如何使用 onResourceReceived() 方法。

var wpage = require('webpage').create(); 
wpage.onResourceReceived = function(response) { 
   console.log(JSON.stringify(response));   
} 
wpage.open('https:///tasks/prompt.html', function(status) {  
}); 

上述程式生成了以下的 輸出

{"body":"","bodySize":231,"contentType":"text/html","headers":[{"name":"Date",
"value":"Sun, 07 May 2017 12:59:17 GMT"},{"name":"Server","value":"Apache/2.4.17 
(Win32) OpenSSL/1.0.2d PHP/5.6.23"},{"name":"Last-Modified","value":"Sun, 
07 May 2017 12:48:14 GMT"},{"name":"ETag","value":"\"e754eee893517e5\""},
{"name":"Accept-Ranges","value":"bytes"},{"name":"ContentLength","value":"231"},
{"name":"Keep-Alive","value":"timeout=5, max=100"},{"name":"Connection","value":
"Keep-Alive"},{"name":"ContentType","value":"text/html"}],"id":1,"redirectURL":null,
"stage":"start","status": 200,"statusText":"OK","time":"2017-05-07T12:59:17.440Z",
"url": "https:///tasks/prompt.html"} 

{"contentType":"text/html","headers":[{"name":"Date","value":"Sun, 
07 May 2017 12:59:17 GMT"},{"name":"Server","value":"Apache/2.4.17 
(Win32) OpenSSL/1.0.2d PHP/5.6.23"},{"name":"Last-Modified","value":
"Sun, 07 May 2017 12:48:14 GMT"},{"name":"ETag","value":"\"e7-54eee893517e5\""},
{"name":"AcceptRanges","value":"bytes"},{"name":"Content-Length","value":"231"},
{"name":"KeepAlive","value":"timeout=5, max=100"},{"name":"Connection",
"value":"KeepAlive"},{"name":"Content-Type", "value":"text/html"}],
"id":1,"redirectURL":null,"stage":"end","status":200,"statusText":"OK",
"time":"2017-0507T12:59:17.486Z","url":"https:///tasks/prompt.html"}
phantomjs_webpage_module_events_callbacks.htm
廣告
© . All rights reserved.