简体   繁体   中英

Open URL after success in Ajax function

I am trying to open an URL from an Ajax function, but the URL is not called.

This is my code:

$(document).on( "click",".btndriver", function() {
      var id = $(this).attr("id");
      var nombre = $(this).attr("nombre");

      swal({   
        title: "Select Driver?",   
        text: "Select Driver? : "+nombre+" ?",   
        type: "warning",   
        showCancelButton: true,   
        confirmButtonColor: "#DD6B55",   
        confirmButtonText: "GO",   
        closeOnConfirm: true }, 
        function(){   
          var value = {
            id: id
          };
          $.ajax(
          {
            url : "ondemand_driver.php",
            type: "POST",
            data : value,
            success: function() {
              window.location(url); 
            }
          });
        });
    });

What is wrong there?

You can't just call an object property key like that. It's not a variable.

Just do this:

var url = "ondemand_driver.php";

$.ajax({
    url : url,
    type: "POST",
    data : value,
    success: function() {
        window.location = url; 
    }
});

Declared the url as variable out of ajax function

var url = "ondemand_driver.php";  
$.ajax(
      {
        url : url,
        type: "POST",
        data : value,
        success: function() {
          window.location(url); 
        }
      });

its work fine.

您需要将url定义为变量,只有ajax请求成功时才会打开url。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM