为什么 Angular HttpClient 将 "\n" 替换为 "↵"
Why does Angular HttpClient replace "\n" with "↵"
发送一个带有 HttpClient 的 POST 主体,它要么是一个字符串,要么是一个以字符串作为值的对象,将用“↵”替换任何出现的“\n”。这主要发生在 Chrome 73。在 firefox 中,在检查器中查看网络调用时,“↵”似乎显示为“”。
我尝试过使用 JSON.stringify 和 JSON.parse 并将“↵”替换为“\n”,但无济于事。
Stackblitz:https://stackblitz.com/edit/angular-uzxank
我希望浏览器检查器中显示的 POST 请求正文使用“\n”而不是“↵”。
这不是 Angular 的 HTTP 客户端特有的。这只是 Chrome 格式化字符串中换行符的显示方式。
查看下面的演示。
document.getElementsByTagName('button')[0].onclick = () =>
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: 'title',
body: 'foo\nbar',
userId: 1
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => console.log(json))
Open Dev Tools. Then click:<br>
<button>click me</button><br>
Now check the HTTP call (the one with 201) in the networks tab<br>
Notice the line break is still shown as "↵" in Chrome.<br>
Notice also that the "\n" is properly transmitted, as shown by the response object's "body" field.
发送一个带有 HttpClient 的 POST 主体,它要么是一个字符串,要么是一个以字符串作为值的对象,将用“↵”替换任何出现的“\n”。这主要发生在 Chrome 73。在 firefox 中,在检查器中查看网络调用时,“↵”似乎显示为“”。
我尝试过使用 JSON.stringify 和 JSON.parse 并将“↵”替换为“\n”,但无济于事。
Stackblitz:https://stackblitz.com/edit/angular-uzxank
我希望浏览器检查器中显示的 POST 请求正文使用“\n”而不是“↵”。
这不是 Angular 的 HTTP 客户端特有的。这只是 Chrome 格式化字符串中换行符的显示方式。
查看下面的演示。
document.getElementsByTagName('button')[0].onclick = () =>
fetch('https://jsonplaceholder.typicode.com/posts', {
method: 'POST',
body: JSON.stringify({
title: 'title',
body: 'foo\nbar',
userId: 1
}),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
.then(response => response.json())
.then(json => console.log(json))
Open Dev Tools. Then click:<br>
<button>click me</button><br>
Now check the HTTP call (the one with 201) in the networks tab<br>
Notice the line break is still shown as "↵" in Chrome.<br>
Notice also that the "\n" is properly transmitted, as shown by the response object's "body" field.