2017-08-08 20 views
0

Ich möchte in eine PHP-Datei umleiten, die mit Ajax aufgerufen wird. In der Datei schreibe ich die Umleitung. Der Umleitungscode funktioniert jedoch nicht. Ajax PHP Redirect auf eine URL

Dies ist der Code für die Ajax-Datei:

<?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"; 
} 
?> 

Dies ist der Ajax-Aufruf Ich benutze:

<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> 
+1

Sie können nicht umleiten aus einer ajaxed Seite – madalinivascu

+0

Ihre Ajax-Antwort sollte Anweisungen für Ihren Rückruf enthalten umleiten, dh die URL in der JSON – Scuzzy

+0

Redirection sein sollte, kann nie in einem passieren AJAX. Sie müssen den Umleitungscode in 'success: function (data) {....' schreiben –

Antwort

1

Redirect mit js in der Erfolgsfunktion

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

Anstelle von:

header("Location: jobs.php"); 

innerhalb PHP-Code, geben Sie die URL in response zum Ajax. Und verwenden Sie window.location in success wie:

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