角/离子处理成功200作为一个错误 [英] Angular/Ionic handling successful 200 as an error

查看:149
本文介绍了角/离子处理成功200作为一个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Google云端函数,当我将数据返回给我的应用程序时,angular默认将其作为错误处理。

这里是我的云端函数调用返回使用节点:$ b​​ $ b我已经尝试添加大括号到有效载荷,但它没有帮助。

 函数(error,response,body){
let payload = parser.toJson(body,parserOptions);
console.log(有效载荷)
res.status(200).send(有效载荷);





$在这个回调的控制台日志中,我得到:

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

这是我期望在Angular / Ionic代码中获得的数据。
在Postman中进行测试也是如此。
$ b $ p

 <$ c $ b $返回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。这看起来像是类似于angularjs 的CORS错误,在 Angular 2的答案



解决方案是添加一个头部服务器端来处理CORS。 p>

I'm using ionic with a google cloud function and when I return data to my application angular defaults to handling it as an error.

Here's my cloud function call back using node: I have already tried adding curly braces to payload and but it didn't help.

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

In the console logs for that call back I get:

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

Which is the data I expect to receive in my Angular/Ionic code. Testing this in Postman works too.

Angular Code:

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)
        }
    )

I get an error status of 0 and error message of null.

解决方案

This looks like a CORS error described similarly here for angularjs and with more explanation in this answer for Angular 2

The solution is adding a header server side to handle CORS.

这篇关于角/离子处理成功200作为一个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆