且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

HTTPClient POST 尝试解析非 JSON 响应

更新时间:2022-12-04 08:34:29

您将 responseType: 'text' 放在了 httpOptions 的错误部分 - 它应该坐在headers外面,像这样:

You've put responseType: 'text' in the wrong section of your httpOptions - It should sit outside of headers, like so:

private httpOptions = {
  headers: new HttpHeaders({
    'Accept': 'text/html, application/xhtml+xml, */*',
    'Content-Type': 'application/x-www-form-urlencoded'
  }),
  responseType: 'text'
};

根据您之前的情况,responseType 的请求标头被发送到服务器,而不是简单地向 Angular 发出指令以实际将响应视为文本.

With what you had before, a request header of responseType was being sent to the server, rather than simply having an instruction to Angular to actually treat the response as text.