1. 程式人生 > >ajax 中 請求 post方式請求url

ajax 中 請求 post方式請求url

<script>
        $(function(){
            var oRequest = getRequest();
            $('.pay-bar .pri').html('&yen;&nbsp;' + oRequest.payAmount/100);
            function getData(orderNo, payAmount, merchantId, userId, token, merchantType){
                var obj = {
                    orderNo: orderNo,
                    payAmount: payAmount,
                    merchantId: merchantId,
                };
                var sign = mapAll (obj);
                $.ajax({
                    url: testUrl + '/**/**.do',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        orderNo: orderNo,
                        payAmount: payAmount,
                        merchantId: merchantId,                 
                        sign: sign
                    }
                })
                .done(function(data) {
                    console.log(data);
                    // GET方式
                    location.href = data.data.payUrl + '?orderMap={"' + oRequest.orderNo + '":"' + oRequest.payAmount +'"}&storeNo=' + oRequest.merchantId + '&source=jxw&notifyUrl=' + data.data.notifyUrl + '&backUrl=' + data.data.backUrl + '&sign=' + data.data.sign;
                    // POST方式
                    var $form = $(document.createElement('form')).css('display', 'none').attr('method', 'POST').attr('action', data.data.payUrl);
                    var val0 = '{"' + oRequest.orderNo + '":"' + oRequest.payAmount +'"}';
                    var $input0 = $(document.createElement('input')).attr('name', 'orderMap').val(val0);
                    var $input1 = $(document.createElement('input')).attr('name', 'storeNo').val(oRequest.merchantId);             
                    $form.append($input0).append($input1);
                    $('body').append($form);
                    $form.submit();
                })
                .fail(function(data) {
                    console.log("error");
                    console.log(data);
                })
                .always(function() {
                    console.log("complete");
                });
                
            }
            $('.pay-bar .pay-confirm').on('click', function(event) {
                event.preventDefault();
                getData(oRequest.orderNo, oRequest.payAmount, oRequest.merchantId, oRequest.userId, oRequest.token, oRequest.merchantType);
            });
        });
    </script>