javascript - 提交表单时 Angular 事件成功打开弹​​出窗口

标签 javascript jquery angularjs form-submit popupwindow

我有一个页面,它通过 angular $http.post(..) 将数据发布到 ASPNET MVC 页面 通话结束后,我必须提交表格。

基本上我会做以下事情:

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

当我做 document.forms["Checkpay"].submit();我方面有一个重定向 到进入我的表单的属性操作的页面,而不是我得到一个我将重定向的页面的弹出窗口。

我已经尝试将函数 payTest() 置于“成功”之外并且它起作用了然后我认为问题是 对象 $http(或 ajax)如何处理“成功”委托(delegate)。

问题是:当调用 $http.post(....) finish 时,无论如何要重定向页面?

谢谢

最佳答案

您的 payTest 函数是否执行?

我会这样做:

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

    }; 

}

更新:我将 $http 调用的语法更改为我一直使用的语法。

关于javascript - 提交表单时 Angular 事件成功打开弹​​出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22168080/

相关文章:

jquery - 更改 div 背景 jquery

javascript - 在将字符串设置为文本区域的值之前是否必须对其进行转义?

javascript - 带有 html 输入控件的动态生成的表行在回发时消失

javascript - ngif 中的 Angular 自定义指令 - 设置其绝对儿子的宽度

javascript - Angular - 当 observable 完成时,做一些事情

javascript - 使用 e.target.name setState 意外标记

Javascript OOP 和类

javascript - 从当前 URL 获取#id

javascript - AngularJS - 如何访问对象中的数组以在 ng-bind 中使用

javascript - 在 UI 路由器状态更改时更新父范围