Question

I have a page which post data to a ASPNET MVC page by angular $http.post(..) and when the calling finish I have to submit a form.

Basically I do the following:

<html ng-app>
<body data-ng-controller="testController">
    <form id="Checkpay" name="Checkpay" style="margin: 0" method="post" action="https://www.sandbox.paypal.com/cgi-bin/webscr" target="_blank" class="ng-pristine ng-valid">
        <input type="hidden" name="quantita" id="qtytext" value="0">
        <input type="hidden" name="cmd" value="_xclick">
        <input type="hidden" name="item_name" value="Le Spose di Nika">
        <input type="hidden" name="business" value="TEST@TEST.it">
        <input type="hidden" name="currency_code" value="EUR">
        <input type="hidden" name="cancel_return" id="cancel_return" value="">
        <input type="hidden" name="return" id="return" value="">
        <input type="hidden" name="no_shipping" value="1">

        <input type="hidden" id="H_amount" name="amount" value="719.80">
        <input type="hidden" id="H_first_name" name="first_name">
        <input type="hidden" id="H_address1" name="address1">
        <input type="hidden" id="H_city" name="city">
        <input type="hidden" id="H_state" name="state">
        <input type="hidden" id="H_zip" name="zip">
        <input type="hidden" id="H_email" name="email">
        <input type="hidden" id="Country" name="country">
        <input type="hidden" id="charset" name="charset" value="utf8">
        <input type="hidden" id="rm" name="rm" value="2">
        <input type="hidden" id="notify_url" name="notify_url" value="">

        <input id="Submit1" type="submit" value="submit_post" />
    </form>
    <input id="Submit2" type="button" data-ng-click='pay()' value="js_post" />

</body>



</html>

<script src="http://localhost:27433/Scripts/jquery-1.7.1.min.js"></script>
<script src="http://localhost:27433/Scripts/jquery-ui-1.8.20.min.js"></script>
<script src="http://localhost:27433/Scripts/angular.js"></script>
<script>

    function test() {

    }

    function testController($scope, $http) {
        var URL = "http://backend.MYSITE.com/";

        $scope.payTest = function () {


            var PAYPAL_URL_RELEASE = "https://www.paypal.com/cgi-bin/webscr";
            var PAYPAL_URL_SANDBOX = "https://www.sandbox.paypal.com/cgi-bin/webscr";

            $('#cancel_return').val(URL + '/ErrorPay');
            $('#return').val(URL + '/Success');
            $('#notify_url').val(URL + '/PayPalReceiverIPN.ashx?idOrder=');
            document.forms["Checkpay"].action = PAYPAL_URL_RELEASE;
            document.forms["Checkpay"].submit();

            console.log('paytest is executed!');
        }

        $scope.pay = function () {

            ////$.post(  URL + "/Order/Create", JSON.stringify({ newOrder: 1 }))
            ////    .success(function (datdata, status, headers, configa) {
            ////        console.log(' $.post success');
            ////        $scope.payTest();
            ////    }).error(function (data, status, headers, config) {
            ////        console.log(' $.post failure');
            ////    });


            $http.post(
               URL + "/Order/Create", JSON.stringify({ newOrder: 1 })
                 ).success(function (datdata, status, headers, configa) {
                     console.log('http success');
                     $scope.payTest();
                 }).error(function (data, status, headers, config) {
                     console.log('http failure');
                 });
        }
    }
</script>

When I do document.forms["Checkpay"].submit(); I aspect to have a redirect to the page which is into the attribute action of my form instead I get a popup of the page which I would redirect.

I have tried to put the function payTest() out of "success" and it works then I supposed that the problem is how the object $http (or ajax) handle the "success" delegate.

the question is: there is anyway to make a redirect of the page when $http.post(....) finish is calling?

thanks

Was it helpful?

Solution 2

Ok, the answer and the explanation to the problem is here Open page in new window without popup blocking

PROBLEM: when I perform form.submit() I get a popup window instead to get a new tab on browser

REASON: I have performed that submiut into a success delegate of ajax calling, that means the user did not start esplicitilly the submit so the browser respond with a popup and with blocked popup if BLOCK IS ON

SOLUTION: we can solve the situation in two way 1) set target ="_self" but we will get the respond of submit into the parent page 2) use a a synchrone flag on ajax calling. Pay attention it seems the angular $http use async requests by hard coded flag

thnak for all of answers

OTHER TIPS

Does your payTest function execute at all?

I would do it like this:

function testController($scope, $http) {

     $scope.pay = function () {
        $http({
             method: 'POST',
             url: 'http://backend.mysite.com/Order/Create',
             data: JSON.stringify({ $scope.newOrder: 1 })
        })
        ['success'](function (data, status, headers, config) {
            console.log('http success');
            $scope.payTest();
        })
        ['error'](function (data, status, headers, config) {
             console.log('http failure');
        });
    };

    $scope.payTest = function() {

         console.log('paytest is executed!');
        // all your stuff

    }; 

}

UPDATE: I changed the syntax of the $http call to what I always use.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top