【问题标题】:Why does Angular HttpClient replace "\n" with "↵"为什么Angular HttpClient用“↵”替换“\n”
【发布时间】:2019-06-09 12:44:40
【问题描述】:

发送带有 HttpClient 的 POST 主体,该主体要么是字符串,要么是具有字符串作为值的对象,会将任何出现的“\n”替换为“↵”。这主要发生在 Chrome 73 中。在 Firefox 中,在 Inspector 中查看网络调用时,“↵”似乎显示为“”。

我尝试使用 JSON.stringify 和 JSON.parse 并将“↵”替换为“\n”,但无济于事。

Stackblitz:https://stackblitz.com/edit/angular-uzxank

我希望浏览器检查器中显示的 POST 请求正文使用“\n”而不是“↵”。

【问题讨论】:

    标签: json angular angular-httpclient


    【解决方案1】:

    这并不是 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.

    【讨论】:

      猜你喜欢
      • 2022-08-03
      • 2018-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多