簡體   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