获取HTTP请求并导航-Chrome



以下是我在Chrome控制台窗口中运行的POST请求示例。

fetch("https://demo.wpjobboard.net/wp-login.php", {
"headers": {
"Host": "demo.wpjobboard.net:443",
"Content-Length": "19",
"Cookie": "wpjb_transient_id=1607759726-1847; wordpress_test_cookie=WP+Cookie+check",
"Content-Type": "application/x-www-form-urlencoded"
},
"body": "log=7887&pwd=789789",
"method": "POST",
}).then(console.log);

我需要在chrome中导航并查看HTML渲染的结果,而不仅仅是在控制台中查看一些复杂的结果。如何做到这一点?

Fetch返回promise,首先您得到的是来自服务器的流式数据。您需要将其转换为文本或JSON,然后可以像使用普通变量一样使用它。

我已经将您的URL和选项移动到单独的变量中,以便将代码集中在获取请求实现上。

const url = `https://demo.wpjobboard.net/wp-login.php`
const opts = {
headers: {
'Cookie': `wpjb_transient_id=1607759726-1847; wordpress_test_cookie=WP+Cookie+check`,
'Content-Type': `application/x-www-form-urlencoded`
},
body: `log=7887&pwd=789789`,
method: `POST`,
}
fetch(url, opts)
.then(res => res.text()) // if you get json as response use: res.json()
.then(html => {
const win = window.open(``, `_blank`)
win.document.body.innerHTML = html
win.focus()
})

相关内容

最新更新