【问题标题】:Angular <-> firebase functions not sending back data trough GET requestAngular <-> firebase 函数不通过 GET 请求发回数据
【发布时间】:2018-08-31 04:26:28
【问题描述】:

在我的 Angular 项目中,我有:

this.htpClien.get(this.baseUrsl+"createEmployee",{params:{email:"sdfsd@dffdf.com",password:"sdafw22"}}).subscribe(sub=>{

  console.log("respons"+ sub)},error=>{console.log('error')}

在我的 firebase 函数中:

exports.createEmployee = functions.https.onRequest((request, response)=>{

response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

    if (request.method === 'GET') {
        let email = request.query.email;
        let password = request.query.password;

            admin.auth().createUser({
                email: email,
                emailVerified: false,
                password: password,
                disabled: false
            }).then(succes => {
                response.send(succes.uid)

            }).catch(er => {
                response.send(er)
            })
    }
});

用户已创建,但我无法使用 userID 获得对我的角度项目的响应,我得到的只是角度请求中调用的错误 ("console.log('error'))。

我试图把它放在我的角度请求中:

headers = new HttpHeaders();
this.headers.append('Allow-Control-Allow-Origin', '*');
this.headers.append('Content-Type', 'application/x-www-form-urlencoded');

然后在请求中添加“标头”:

this.htpClien.get(this.baseUrsl+"createEmployee",{params:{email:"sdfsd@dffdf.com",password:"sdafw22"},headers:this.headers})

但这根本不起作用,甚至没有创建用户 - 猜测这是“Access-Control-Allow-Origin”的问题,但我在stackoverflow上查看了关于该主题的所有答案,但仍然无法弄清楚这个:S

【问题讨论】:

    标签: angular httprequest google-cloud-functions


    【解决方案1】:

    我终于弄清楚了,对于任何有同样问题的人,Yu 必须指定响应类型,所以在我的情况下,它应该如下所示:

    this.htpClien.get(this.baseUrsl+"createEmployee",{params:{email:"sdfsd@dffdf.com",password:"sdafw22"},responseType: 'text'})
    

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多