node.js - Express 服务器、paypal 结帐、res.redirect 和 Uncaught SyntaxError : Unexpected token < in JSON at position 0

标签 node.js express paypal

我正在尝试使用 Paypals Node SDK 按照他们的示例代码进行支付重定向:

router.post('/', (req,res,next) => {

    //create a payment: 
    var payReq = JSON.stringify({
        intent:'sale',
        payer:{
            payment_method:'paypal'
        },
        redirect_urls:{
            return_url:'http://localhost:3000/pay/payment', // this is where they get redirected after paying.
            cancel_url:'http://localhost:3000/'
        },
        transactions:[{
            amount:{
            total: req.headers.amount,
            currency:'AUD'
            },
            description:'eventID: ' + req.headers.eventid + ' userID: ' + req.headers.userid
        }]
    });

    paypal.payment.create(payReq, (error, payment) => {
        var links = {};

        if(error){
            console.log('something went wrong with paypal');
            console.error(JSON.stringify(error));
        } else {
            // Capture HATEOAS links
            payment.links.forEach(function(linkObj){
                links[linkObj.rel] = {
                    href: linkObj.href,
                    method: linkObj.method
                };
            })

            // If redirect url present, redirect user
            if (links.hasOwnProperty('approval_url')){
                //REDIRECT USER TO links['approval_url'].href
                console.log('redirecting to ' + JSON.stringify(links['approval_url']));
                var redirectUrl = JSON.stringify(links['approval_url'].href);
                res.redirect(301, redirectUrl);
            } else {
                console.error('no redirect URI present');
            }
        }
     });
});

Paypal 的授权有效,他们以 JSON 格式向我发送此信息,我将其用作重定向路由,该路由从上面的语句中被控制台记录:

redirecting to {"href":"https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-The-token-goes-here","method":"REDIRECT"}

但是在我执行 res.redirect 之后,我的浏览器控制台立即吐出这个错误并且没有发生重定向,它指向我的 vendor.ca6865e 中的这段代码….bundle.js:1787:

t.prototype.json=function(){return"string"==typeof this._body?JSON.parse(this._body):this._body instanceof ArrayBuffer?JSON.parse(this.text()):this._body},t.prototype.text=function(){return this._body instanceof i.a?this._body.toString():this._body instanceof ArrayBuffer?String.fromCharCode.apply....

Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at e.t.json (vendor.ca6865e….bundle.js:1787)
    at e.project (main.c0aa810….bundle.js:1)
    at e._next (vendor.ca6865e….bundle.js:474)
    at e.next (vendor.ca6865e….bundle.js:1)
    at XMLHttpRequest.h (vendor.ca6865e….bundle.js:1780)
    at t.invokeTask (vendor.ca6865e….bundle.js:2950)
    at Object.onInvokeTask (vendor.ca6865e….bundle.js:854)
    at t.invokeTask (vendor.ca6865e….bundle.js:2950)
    at e.runTask (vendor.ca6865e….bundle.js:2950)
    at XMLHttpRequest.invoke (vendor.ca6865e….bundle.js:2950)


ORIGINAL STACKTRACE:
    t.handleError   @   vendor.ca6865e….bundle.js:1640
next    @   vendor.ca6865e….bundle.js:1068
e.object.i  @   vendor.ca6865e….bundle.js:1117
e.__tryOrUnsub  @   vendor.ca6865e….bundle.js:1
e.next  @   vendor.ca6865e….bundle.js:1
e._next @   vendor.ca6865e….bundle.js:1
e.next  @   vendor.ca6865e….bundle.js:1
e.next  @   vendor.ca6865e….bundle.js:35
e.emit  @   vendor.ca6865e….bundle.js:1117
t.triggerError  @   vendor.ca6865e….bundle.js:854
onHandleError   @   vendor.ca6865e….bundle.js:854
t.handleError   @   vendor.ca6865e….bundle.js:2950
e.runTask   @   vendor.ca6865e….bundle.js:2950
invoke  @   vendor.ca6865e….bundle.js:2950


SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at e.t.json (vendor.ca6865e….bundle.js:1787)
    at e.project (main.c0aa810….bundle.js:1)
    at e._next (vendor.ca6865e….bundle.js:474)
    at e.next (vendor.ca6865e….bundle.js:1)
    at XMLHttpRequest.h (vendor.ca6865e….bundle.js:1780)
    at t.invokeTask (vendor.ca6865e….bundle.js:2950)
    at Object.onInvokeTask (vendor.ca6865e….bundle.js:854)
    at t.invokeTask (vendor.ca6865e….bundle.js:2950)
    at e.runTask (vendor.ca6865e….bundle.js:2950)
    at XMLHttpRequest.invoke (vendor.ca6865e….bundle.js:2950)

最佳答案

Derp,是 JSON.stringify 破坏了它。事实证明这是没有必要的。

关于node.js - Express 服务器、paypal 结帐、res.redirect 和 Uncaught SyntaxError : Unexpected token < in JSON at position 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42221417/

相关文章:

javascript - 用于更改 NVM Node 版本的 Bash 脚本

node.js - Node.js+express+session

node.js 我可以为同一个项目使用多个 ssl 证书和 key 吗?如何使用?

php - 如何使用 PHP 解析来自 PayPal API 的 SOAP 响应?

php - PayPal 付款 - HTTP 响应代码 400

php - PayPal 沙箱 IPN 不再响应

javascript - Node js + jade 打印在 jade 文件中传递变量

javascript - Nodejs/Expressjs 应用结构

node.js - 无法使用 Node 在angular2中发布文件

javascript - Socket.io 清理传入数据 (xss)