Ajax成功后,window.location.href无法正常工作

时间:2019-07-19 11:06:56

标签: javascript php jquery codeigniter razorpay

在这段代码中,我使用的是razorpay付款网关api,其中的付款已成功完成,但在成功执行ajax调用后无法重定向。因此,如何在订单页面中使用window.location.href重定向页面?请帮助我。

$(document).ready(function() {
  $('#buy_now').click(function(e) {
    var total_amount = $(".total_amount").val();
    var product_id = $(".product_id").map(function() {
      return this.id;
    }).get().join(",");
    var new_uid = $("#new_uid").val();
    var user_id = $("#uid").val();
    var user_email = $(".user_email").attr("id");
    var options = {
      "key": "************************",
      "amount": (1 * 100), // 2000 paise = INR 20
      "name": "MyApp",
      "description": "Payment",
      "image": "https://www.MyApp.com/img/logo/logo.png",
      "handler": function(response) {
        $.ajax({
          url: '<?php echo base_url() ?>razorPaySuccess',
          type: 'post',
          dataType: 'json',
          data: {
            "razorpay_payment_id": response.razorpay_payment_id,
            "total_amount": total_amount,
            "product_id": product_id,
            "new_uid": new_uid,
            "user_email": user_email,
            "user_id": user_id
          },
          success: function(data) {
            window.location.href = "<?php echo base_url(); ?>order";
          }
        });
      },
      "theme": {
        "color": "#528FF0"
      }
    };
    var rzp1 = new Razorpay(options);
    rzp1.open();
    e.preventDefault();
  });
});

5 个答案:

答案 0 :(得分:0)

将您的网址存储在变量中,然后调用此行。

var url = "your current url";
$(location).attr('href',url);

答案 1 :(得分:0)

到目前为止,我还没有注意到您的代码有任何错误..您只需确保将其转到success: function()并使用window.location.replace而不是window.location.href。因此它将替换当前的网址,并将您重定向到成功页面。

window.location.replace = "<?php echo base_url(); ?>order";

如果上面的代码仍然不起作用。您的e.preventDefault()可能会导致您无法重定向到成功页面,因此请确保相应地使用它。

答案 2 :(得分:-1)

window.location.replace('ADDRESS')可能对您有用

答案 3 :(得分:-1)

这样做:-

var path = "<?php echo base_url(); ?>";
window.location.href = path+"order";

答案 4 :(得分:-2)

<div class="magic-slide mt-active" data-magic-slide="zoom">
    <a id="MagicZoomPlusImage286" class="MagicZoom" href="https://www.blizz-z.de/media/catalog/product/cache/2/image/0dc2d03fe217f8c83829496872af24a0/f/l/fliesenkleber-proflex-fix-1303_1.jpg" data-options="selectorTrigger:hover;textHoverZoomHint:Hovern zum Zoomen;textClickZoomHint:Berühren zum Zoomen;textExpandHint:Vergrößern;"
        data-mobile-options="textHoverZoomHint:Berühren zum Zoomen;textClickZoomHint:Doppeltippe zum Zoomen;textExpandHint:Vergrößern;">
        <figure class="mz-figure mz-hover-zoom mz-ready"><img itemprop="image" src="https://www.blizz-z.de/media/catalog/product/cache/2/image/450x450/0dc2d03fe217f8c83829496872af24a0/f/l/fliesenkleber-proflex-fix-1303_1.jpg" alt="Fliesenkleber proflex fix Schnell-Fliesenkleber - nach 3 Stunden begehbar"
                style="max-width: 450px; max-height: 450px;">
            <div class="mz-lens" style="top: 0px; transform: translate(-10000px, -10000px); width: 122px; height: 122px;"><img src="https://www.blizz-z.de/media/catalog/product/cache/2/image/450x450/0dc2d03fe217f8c83829496872af24a0/f/l/fliesenkleber-proflex-fix-1303_1.jpg" style="position: absolute; top: 0px; left: 0px; width: 349px; height: 349px; transform: translate(-1px, -132px);"></div>
            <div
                class="mz-loading"></div>
<div class="mz-hint mz-hint-hidden"><span class="mz-hint-message">Vergrößern</span></div>
</figure>
</a>
</div>