Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 如何在http get或post上调用函数?_Angularjs_Node.js_Http_Express_Asynchronous - Fatal编程技术网

Angularjs 如何在http get或post上调用函数?

Angularjs 如何在http get或post上调用函数?,angularjs,node.js,http,express,asynchronous,Angularjs,Node.js,Http,Express,Asynchronous,我相信这是一个愚蠢的问题,但我对后端非常陌生,所以请原谅我 我正在用express/node构建一个angularjs应用程序,并尝试集成PayPal(作为一个应用程序),我想从angular controller调用SDK上的pay方法,我的工作如下: 点击按钮: // controller $scope.pay = function(amount) { PaymentFactory.doPayment(amount); } 付款工厂: // PaymentFactory return

我相信这是一个愚蠢的问题,但我对后端非常陌生,所以请原谅我

我正在用express/node构建一个angularjs应用程序,并尝试集成PayPal(作为一个应用程序),我想从angular controller调用SDK上的pay方法,我的工作如下:

点击按钮:

// controller
$scope.pay = function(amount) {
  PaymentFactory.doPayment(amount);
}
付款工厂:

// PaymentFactory
return {
    doPayment: function(amount) {
        $http.get("../../../server/payments/paypal.js")
            .then(function(response) {
                console.log( response );
            })
    }
}
那么服务器端文件如下所示:

require('paypal-adaptive');
var app = require('../../server.js');
var PayPal = require('paypal-adaptive');

var paypalSdk = new PayPal({
    userId:    'userid',
    password:  'password',
    signature: 'signature',
    sandbox:   true //defaults to false 
});

var payload = {
    requestEnvelope: {
        errorLanguage:  'en_US'
    },
    actionType:     'PAY_PRIMARY',
    currencyCode:   'GBP',
    feesPayer:      'EACHRECEIVER',
    memo:           'Chained payment example',
    cancelUrl:      'returnUrl,
    returnUrl:      'cancelUrl',
    receiverList: {
        receiver: [
            {
                email:  'email1',
                amount: '3.40',
                primary:'true'
            },
            {
                email:  'email2',
                amount: '1.20',
                primary:'false'
            }
        ]
    }
};

paypalSdk.pay(payload, function (err, response) {
    if (err) {
        console.log(err);
    } else {
        // Response will have the original Paypal API response 

        // But also a paymentApprovalUrl, so you can redirect the sender to checkout easily 
        console.log('Redirect to %s', response.paymentApprovalUrl);

        return response;
    }
});

当然,get请求只返回一个服务器端文件内容字符串,我理解上面的操作为什么不起作用,但不确定如何使其起作用。我的目标是从angular工厂调用PayPal SDK并获取响应,以便将用户重定向到URL。一个直接的解决方案会很有帮助,但更重要的是,我需要指向一些原则的指针,这些原则我在这里并不理解,因为我们应该如何在用户操作时调用函数来从服务器端获取这些数据。我尝试过搜索,但我不知道搜索中使用的语言。

您需要做的就是使用curl(node curl npm module)。使用curl将帮助您将数据发布到paypal url,并获得回复。现在,您需要处理来自paypal的响应,并相应地生成您自己的响应,以供http方法接收