首页 文章

Angular 4 http发布请求google captcha api失败

提问于
浏览
0

我正在尝试使用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}

当我检查我的网络选项卡时,它似乎成功:

Network

Network

这是我的代码:

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 回答

  • 0

    好的结果是只是谷歌错过了cors Headers ....我能够通过让我自己的api将请求发送给谷歌然后将其返回给我来解决这个问题 .

相关问题