gpt4 book ai didi

javascript - 记录XHR的请求负载

转载 作者:行者123 更新时间:2023-12-03 01:02:09 26 4
gpt4 key购买 nike

每当发出 XHR 请求有效负载时,我想在 Chrome 的控制台中打印有效负载,我该如何执行此操作?任何和所有的想法都非常受欢迎。

最佳答案

也许我没有理解你的问题,要打印变量来控制台你需要下一个代码

const method = 'POST';
const requestUrl = '/';
// Payload as a JSON object
const payload = { name: 'test' };
// Form the http request as a JSON type
const xhr = new XMLHttpRequest();
xhr.open(method, requestUrl, true);
xhr.setRequestHeader('Content-Type', 'application/json');

// When the request comes back, handle the response
xhr.onreadystatechange = () => {
if (xhr.readyState === XMLHttpRequest.DONE) {
const statusCode = xhr.status;
const responseReturned = xhr.responseText;
// Print the response to the chrome console
console.log(responseReturned);
}
};

// Send the payload as JSON
const payloadString = JSON.stringify(payload);
// Print the payloadString to chrome console
console.log(payloadString);
xhr.send(payloadString);

关于javascript - 记录XHR的请求负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52577213/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com