我正在尝试在Angular中发出请求,我知道HTTP响应不是JSON而是文本.但是,Angular似乎期待JSON响应,因为错误如下:
SyntaxError:XMLHttpRequest.c中JSON.parse()位置0的JSON中的意外标记<
以及
解析http:// localhost:9时出现 Http失败...
这是post方法:
return this.http.post(this.loginUrl, this.createLoginFormData(username, password), this.httpOptions)
.pipe(
tap( // Log the result or error
data => console.log(data);
error => console.log(error)
)
);
Run Code Online (Sandbox Code Playgroud)
和标题.
private httpOptions = {
headers: new HttpHeaders({
'Accept': 'text/html, application/xhtml+xml, */*',
'Content-Type': 'application/x-www-form-urlencoded',
responseType: 'text'
},
) };
Run Code Online (Sandbox Code Playgroud)
我认为这responseType: 'text'足以让Angular期望非JSON响应.