我正在使用以下角度代码将字符串发布到后端服务器。我收到响应错误。
const headers = new HttpHeaders({'Content-Type':'application/json; charset=utf-8'});
@Injectable({
providedIn: 'root'
})
export class ApiService {
apiURL = environment.apiUrl;
stockUrl = this.apiURL + '/api/stock';
postStock(stocks) {
this.http.post(this.stockUrl, stocks, {headers: headers})
.subscribe(res => {
console.log("POST call successful value returned in body", res);
},
response => {
console.log("POST call in error", response);
})
}
}
我收到控制台日志“错误中的POST呼叫,发生未知错误”
这是组件代码:
stockdata: Cagr[];
stock: String;
constructor(private api: ApiService){}
post(stock){
this.api.postStock(stock);
console.log("this is the stock I posted", stock);
}
控制台日志的确在html文件中发布了准确的库存模型。
为什么这不起作用?
答案 0 :(得分:1)
参考:Angular 4: Unknown error after POST request to REST api on localhost