繁体   English   中英

来自 HttpClient POST 请求的 Angular 6 HTML 响应

[英]Angular 6 HTML response from HttpClient POST request

我正在尝试进行第三方授权。 当我向服务器发出 POST 请求时,它会发送 HTML 作为响应。

标题

private httpOptions = {
    headers: new HttpHeaders({
        'Accept': 'text/html',
        'Content-Type': 'application/json'
}),
    responseType: 'text',
};

API调用

return this.http.post(this.authUrl, this.httpOptions)
    .subscribe(data => {
        console.log(data);
    });

我收到一个语法错误:

JSON.parse 位置 0 处的 JSON 中的意外标记 <

 private httpOptions = {
    headers: new HttpHeaders({
        'Accept': 'text/html',
        'Content-Type': 'application/json'
    }),
    responseType: 'text'
};

你的 HTTP 选项在responseType:'Text'后面多了一个逗号

return this.http.post(this.authUrl,null, this.httpOptions)
        .subscribe(data => {
            console.log(data);
        });

此外, HttpClientModule 的HTTP POST 将有效负载作为 POST 调用中的第二个参数。

在我看来,放置响应类型的最佳方法是当您调用 http 帖子时,这将阻止 HttpClient 尝试将空响应解析为 JSON 对象。

 this.http.put(
 url,
 data,
 {
    headers: new HttpHeaders().set('Content-Type', 'application/json'),
    responseType: 'text' 
 }
 ).subscribe(  ....   );

有帖子

return this.http.post(url, params, {headers : headers, withCredentials : true,responseType: 'text'});

暂无
暂无

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

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