javascript - 记录 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);
推荐阅读
- ios - 将 UIImage 坐标转换为 CIImage 坐标
- python - 我如何将这个带有选择的字母数组变成可能的单词列表?
- embed - 使用实时更新嵌入来自另一个网站的表格
- reactjs - 如何使用按钮更改 antd 步骤状态
- python - 无法使用请求从网页中获取表格内容
- caching - FileContentResult 有时会缓存
- c# - 发送 HTTP 请求不适用于 WebProxy
- rust - 带有 HRTB 的 Rust 类型参数
- django - Django:如何从颤动中读取使用 Dio 发送的图像
- django - 我可以在 django 中的 model.py 文件中添加任何条件吗?