私は私の悪い英語をお詫び申し上げます。
私はajaxリクエストでphpファイルを作成しています。jsonレスポンスはの形式で提供されていますが、場合によってはリダイレクトすることもできます。この場合はページのリダイレクトが必要です。
手伝っていただけませんか。ありがとうございます。
PHPファイルの例:
<?php
$status = $_POST["status"];
if($status == "a"){
// return json response
}else{
echo "<form action="http://www.url.com">..</form><script type="text/javascript">form.submit();</script>";
}
?>
例JSファイル:
$.ajax({
type: "POST",
url: "http://www.my_php_file.com"
});
回答:
回答№1の場合は-1応答データが有効なJSONかどうかを検出する必要があります。
$.ajax({
type: "POST",
url: "http://www.my_php_file.com",
success: checkAJAX
});
function checkAJAX(data)
{
var response = $.parseJSON(data);
if(typeof response === "object")
{
}
else
{
// If the AJAX response is not JSON, append the HTML to the document
$("body").append(data);
}
}
回答№2の場合は1
これを試して。
url: "http://www.my_php_file.com",
success: function(data) {
document.location.href="YouNewPage.php";
}
回答№3の場合は1
成功機能を使用する https://api.jquery.com/jQuery.ajax/
$.ajax({
type: "POST",
url: "http://www.my_php_file.com"
data: { status : statusVar },
success: function(response){
if (response.status == "a"){
$( "#results" ).append( response);
}else{
window.location = "http://www.url.com"
}
});
});
回答№4の場合は-1
JSONとしてエコーしたいHTMLも返します。
if($status == "a"){
// return json response
} else {
echo json_encode(array("redirect" => "<form action="http://www.url.com">..</form><script type="text/javascript">form.submit();</script>"));
}
チェック redirect
ajaxレスポンスでは、
$.ajax({
type: "POST",
dataType: "json",
url: "http://www.my_php_file.com",
success: function(data) {
if(typeof data.redirect !== "undefined") {
$("body").append(data.redirect);
}
}
});
2回注意するだけで、リクエストが失敗した場合にリダイレクトは行われません(no fail
あなたのカジュアルなJSONレスポンスは属性を持っていないと思います redirect
.