jQuery重定向太慢了

时间:2017-12-20 06:17:34

标签: javascript jquery ajax

jQuery重定向太慢,无法连接另一个页面。我尝试过window.location.replacewindow.location.hrefwindow.location。有没有办法让重定向更快?

<form role="form" id="add_name"  method="post"  enctype="multipart/form-data" action="">
  <div class="form-group modal-form">
    <input type="text" class="form-control" id="email_id" name="email_id">
    </div>
    <div class="form-group modal-form">
      <input type="text" class="form-control" id="contact" name="contact" >
      </div>
      <button  type="submit" class="btn btn-popup"  name="submit" id="submit">save</button>
    </div>
  </div>
</form>
<script> 
  $("form#add_name").submit(function(e) {
    e.preventDefault();
    var formData = new FormData(this);
    $.ajax({
      url: "device_process.php",
      type: 'POST',
      data: formData,
      success: function(data) {
        window.location = "thankyou.php";
      },
      cache: false,
      contentType: false,
      processData: false
    });
  });
</script>

2 个答案:

答案 0 :(得分:1)

这可能是因为您的服务器端代码可能需要一些时间来处理请求。在这种情况下,最好添加进度加载器以通知用户他们的请求正在进行中。它很容易实现。请尝试以下代码。这会奏效。希望这可以帮助。感谢

INSIDE BODY:

<div id="loading" style="display: none;"><div>Loading, Please wait...</div></div>

CSS:

#loading {
    position: fixed;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    top: 0;
    left: 0;
    background-color: #DBDBDB;
    opacity: .9;
    -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=90)";
    filter: alpha(opacity=90); 
    z-index: 99999;
    display: none;
}

#loading>div{
    position: fixed;
 top: 50%;
 left: 50%;
 margin-left: -102px;
 width: 205px;
 height: 60px;
 text-align: left;
 background: url('../img/loading.gif') no-repeat; //add a loader image to your local
 margin-top: -30px;
 padding: 8px 0 0 35px;
}

JS:

$(document).ajaxStart(function () {

    $("#loading").fadeIn();

}).ajaxStop(function () {

    $("#loading").fadeOut();

    $(document).foundation();

});

答案 1 :(得分:0)

我使用了jquery Timeout()。function。

以下是我的回答:

<script> 
$("form#add_name").submit(function(e) {
e.preventDefault();   
var formData = new FormData(this);
$.ajax({
    url: "device_process.php", 
    type: 'POST',
    data: formData,
    success: function (data) {
       //  window.location = "thankyou.php?id=<?php echo htmlspecialchars($devices); ?>";

    },
    cache: false,
  contentType: false, 
    processData: false,

});
timeout:2000 , 
window.location = "thankyou.php?id=<?php echo htmlspecialchars($devices);    ?>";
});
</script>