1

そのため、すべてのデータをデータベースに挿入するphpスクリプトにajaxを使用して、メインページのフォームデータを送信しています。終了後、どこかにリダイレクトしたいのですが。だからここに私のajaxがあります:

$('#form').submit(function() {
    if(val.form()) { 
    var $this = $(this);
        $.ajax({
            data: $this.serialize(), // get the form data
            type: "POST",
            url: "scripts/insert.php",
            complete: function(response){
                var redirect = response.getHeader('Location');
                alert(redirect);
            }
        });
    }
    return false;
});

そして、私が持っているphpファイルの最後に:

header("Location: http://www.google.com", true, 401);
exit();

私はchromephpを使用してデバッグするので、実行するとコンソールは次のように出力します:

POST http://www.domain.com/Folder/scripts/insert.php 401 (Authorization Required) jquery.min.js:2

Uncaught TypeError: Object #<Object> has no method 'getHeader' /Folder/:320

では、ajax を使用しているときにスクリプトの最後でリダイレクトするにはどうすればよいでしょうか。ありがとう

4

1 に答える 1

2

それを試してください:

<?php  
ini_set('display_errors', false);  
echo ('Location: http://www.google.com');
?>

それは私のために働いた。

JavaScript:

 $.ajax({
  type: 'POST',
  url: "http://localhost/test.php",
  data: {name: name },
  success: function(output){ window.location = output; }
  dataType: dataType
});
于 2012-10-22T19:39:58.267 に答える