【发布时间】:2019-09-23 13:29:19
【问题描述】:
我在 laravel 中创建了一个 API,并在 postman 中进行了测试,它运行良好。但是当我从角度尝试它时,它适用于返回字符串文本但不适用于 JSON 响应
我在互联网上搜索并找到设置 content-type:application/json 并尝试使用不同的方法在标题中设置内容类型,但问题仍然存在
var obj = JSON.parse('{"email":"ab@gm.com","password":"12345678"}');
//1st type of header
var headers_object = new HttpHeaders().set('Content-Type',
'application/json');
const httpOptions = {
headers: headers_object
};
//2nd type of header
var HTTPOptions = {
headers: new HttpHeaders({
'Accept':'application/json',
'Content-Type': 'application/json'
}),
'responseType': 'application/json' as 'json'
}
return this.http.post<any>(`http://127.0.0.1:8000/api/auth/login`, obj,HTTPOptions ).subscribe(resp => {
console.log(resp);
});
邮递员输出
【问题讨论】: