我正在尝试使用angular 4的http库和google的siteverify api验证我的验证码响应 . 出于某种原因在控制台中打印:
阻止跨源请求:同源策略禁止在https://www.google.com/recaptcha/api/siteverify上读取远程资源 . (原因:缺少CORS Headers 'Access-Control-Allow-Origin') .
和
ERROR Object {_body:error,status:0,ok:false,statusText:“”,headers:Object,type:3,url:null}
当我检查我的网络选项卡时,它似乎成功:
这是我的代码:
import { Injectable } from '@angular/core';
import { Http, URLSearchParams, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class CaptchaService {
captchaUrl = 'https://www.google.com/recaptcha/api/siteverify';
secretKey = 'SECRETKEY';
constructor(private http: Http) { }
verifyCaptcha = function(captchaResponse) {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let body = new URLSearchParams;
body.set('secret', this.secretKey);
body.set('response', captchaResponse);
return this.http.post(this.captchaUrl, body, { headers: headers })
.map((response) => response.json())
.subscribe((data) => console.log(data));
}
}
如果这是一个不起眼的问题,我很抱歉,但我很有棱角分明 .
谢谢
1 回答
好的结果是只是谷歌错过了cors Headers ....我能够通过让我自己的api将请求发送给谷歌然后将其返回给我来解决这个问题 .