最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Open URL after success in Ajax function - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Sep 26, 2017 at 10:03 mvascomvasco 5,1017 gold badges68 silver badges137 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

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

Change this

 window.location(url)

To this

window.location = url; 

Complete Code

var url = "ondemand_driver.php";

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

You need to define url as variable, the url will be opened only if the ajax request is successful.

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.

发布评论

评论列表(0)

  1. 暂无评论