从http.post数据获取console.log

时间:2016-09-01 09:03:41

标签: javascript angularjs

我正在尝试获取连锁网址的控制台日志,我尝试创建一个新变量,然后将其传递给console.log但没有成功。

如何获取我想发布的网址?

        $scope.sendPaymentConfirmation = function (ordernumber) {
        //function sendPaymentConfirmation(ordernumber) {
            $http({
                url: ('http://example.com/api/' + ordernumber), //'http://samedomain.com/GetPersons',
                var posturl = url;
                console.log(posturl);
                method: "POST",
                //data: postData,
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
            .then(function(response) {
                    // success : Thankyou, payment confirmation has been sent to the API server
                    window.alert('SUCCESS - Payment confirmation has been sent to the API server');
                }, 
                function(response) { // optional
                    // failed : ERROR There has been an error sending payment confirmation to the API server
                    window.alert('ERROR - There has been an error sending payment confirmation to the API server');
                }
            );
        }

2 个答案:

答案 0 :(得分:0)

您无法在对象文字中创建变量。你也不能在那里放任何其他任意的JS陈述。

首先创建变量。然后再使用它。

var posturl = 'http://example.com/api/' + ordernumber
console.log(posturl);

$http({
     url: posturl,

答案 1 :(得分:0)

您需要了解对象文字中不能包含语句和分号。如果您不想以@Quentin建议的标准方式执行此操作,那么您可以这样做(立即函数调用):

    $http({
            url: (function(){
                  var posturl = 'http://example.com/api/' + ordernumber; //'http://samedomain.com/GetPersons',
                  console.log(posturl);
                  return posturl;
            })(),
            method: "POST",
            //data: postData,
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })