我正在将设备中的联系人成功获取到我的列表页面中。但是我无法将该特定的联系人姓名和电话号码发布到服务器上。
答案 0 :(得分:0)
在.ts文件中导入以下内容:
import { HttpClient, HttpHeaders } from '@angular/common/http';
constructor(private http: HttpClient) {
let data = { name: "abc", number: 123 };
let body = JSON.stringify(data);
let head = new HttpHeaders();
head.append('Access-Control-Allow-Origin' , '*');
head.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
this.headers.append('Content-Type', 'application/json; charset=utf-8');
this.http.post('YOUR_URL', body, {headers: head}).toPromise().then(response=>{
console.log("------ Success -------"+response);
});
}
如上所示,我导入http来使用http方法和标头,有时也可能会出现CORS错误并定义标头的内容类型。