我试图通过发出get请求从Angular的Gravityform Rest API V.2接收条目,除了我不断收到401 http错误响应,而我的控制台记录(“从源访问'livesite'处的XMLHttpRequest' localsite”已被CORS政策阻止:对预检请求的响应未通过访问控制检查:它没有HTTP正常状态。”)。我已经使用“基本身份验证”进行了请求。
https://docs.gravityforms.com/rest-api-v2/#basic-authentication https://docs.gravityforms.com/rest-api-v2/#get-forms-form-id-entries
我成功地使用Postman应用程序以相同的凭据发出发布/获取请求。我已将SSL添加到我的域中,并在Wordpress中允许“ Access-Control-Allow-Origin:*”。
// Link to form #1
const url = 'https://livesite.com/wp/wp-json/gf/v2/forms/1/entries';
// Base64 key with : and keys are set in environment.ts
const key = btoa(this.publicApiKey + ':' + this.privateApiKey);
// Create HttpHeaders
const headersObject = new HttpHeaders().set('Authorization', 'Basic ' + key);
const httpOptions = { headers: headersObject };
// Get
this.http.get(url, httpOptions)
.subscribe(
response => {
console.log('response', response);
},
error => {
console.log('error', error);
}
);