繁体   English   中英

记录 XHR 的请求负载

[英]log the request payload of XHR

每当生成 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM