Angular/Ionic 处理成功 200 作为错误

Angular/Ionic handling successful 200 as an error

我将 ionic 与 google 云函数一起使用,当我 return 向我的应用程序发送数据时 angular 默认将其作为错误处理。

这是我使用节点的云函数回调: 我已经尝试将花括号添加到有效负载中,但没有帮助。

function (error, response, body) {
    let payload = parser.toJson(body, parserOptions);
    console.log(payload)
    res.status(200).send(payload);
}

在该回调的控制台日志中,我得到:

{ 
    ResponseCode: '0',
    referenceID: '22072017152436718488608295',
    ResponseMessage: 'SUCCESS',
    paymentURL: 'url',
    net_amount: '0',
    invoiceNumber: '0',
    status: '200' 
}

这是我希望在我的 Angular/Ionic 代码中收到的数据。 在 Postman 中进行测试也可以。

Angular代码:

return this.http.post('/api', body, headers)
    .map(res => res.json())
    .subscribe(
        data => {
            console.log('Success')
            console.log(data)
        },
        err => {
            console.log('Error')
            console.log(err.status)
            console.log(err.message)
        }
    )

我收到错误状态 0 和错误消息 null。

这看起来像是描述的 CORS 错误 similarly here for angularjs and with more explanation in this answer for Angular 2

解决方案是添加 header 服务器端来处理 CORS。