當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PhantomJS onResourceReceived()用法及代碼示例


當接收到頁麵請求的資源時調用此回調。它包含 response 作為參數。

響應對象具有以下詳細信息 -

  • Id- 請求資源的數量。

  • URL- 請求的 URL。

  • Time− 包含響應日期的日期對象。

  • Headers- http 標頭。

  • BodySize− 接收到的內容解壓後的大小。

  • ContentType- 內容類型(如果指定)。

  • RedirectURL− 如果有重定向,則為重定向的 URL。

  • Stage− 值為 - 開始和結束。

  • Status- http 代碼狀態,即狀態 200。

  • StatusText- code 200的http狀態文本,可以。

用法

它的語法如下 -

page.onResourceReceived = function(response) {}

示例

下麵的代碼展示了使用onResourceReceived()方法。

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

上述程序生成以下內容output

{"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":"http://localhost/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":"http://localhost/tasks/prompt.html"}

相關用法


注:本文由純淨天空篩選整理自 PhantomJS - onResourceReceived()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。