2017-08-08 130 views
0

我想在使用ajax調用的php文件內重定向。在文件裏我寫了重定向。但重定向代碼不起作用。Ajax PHP重定向到一個網址

這是AJAX文件中的代碼:

<?php 
ob_start(); 
include 'admin/includes/front_controller.php'; 

if (isset($_POST['id'])) { 
    $job_id = intval($_POST['id']); 
    $job_id = 30; 
    $jobdetail = $db->getTableWhere("jobs", "jbid", $job_id); 
    $count = count($jobdetail); 
    if ($count <= 0) { 
     header("Location: jobs.php"); 
     exit(); 
    } 
} else { 
    echo "There is an error processing your request"; 
} 
?> 

這是Ajax調用我使用:在成功的功能

success: function (data) { 
    alert(data); 
    window.location ="jobs.php"; 
}, 
+1

你不能從一個ajaxed頁面 – madalinivascu

+0

你的Ajax響應應該包括指令回調重定向重定向,即URL應該在JSON – Scuzzy

+0

重定向不能內的發生AJAX。您必須在'success:function(data){....'中寫入重定向代碼 –

回答

1

使用重定向JS代替:

header("Location: jobs.php"); 

裏面的php代碼,將網址response返回給ajax。而在success喜歡使用window.location

success: function (response) { 
    window.location = response; 
} 
0

<script type="text/javascript"> 
    $(".job-btn").on('click', function (e) { 
     e.preventDefault(); 
     var id = $(this).data('id'); 
     //alert(id); 
     var url = "<?php echo SITE_URL; ?>" + "jobdetail.php"; 
     var info = 'id=' + id; 
     $.ajax({ 
      type: "POST", 
      url: url, 
      data: info, 
      success: function (data) { 
       alert(data); 

      }, 
      error: function (data) { 
       //alert(data.responseText); 
       //alert("Error occured in showing details"); 
      } 
     }) 

    }); 
</script>