如何使用 jquery ajax 和 php 响应重定向
How to redirect with jquery ajax and php response
我有以下 ajax 和 php。我想根据我使用 php 脚本输出的结果将用户重定向到另一个页面。 Php 成功 returns json 到浏览器,但我无法将响应 url 返回到 js ajax 以进行重定向。它总是将我重定向到 /undefined。我的代码有什么问题?
jQuery.ajax({
type: "POST",
url: "../custom_scripts/anmeldung_zurueckziehen.php",
data: { anmeldung_id: anmeldung_id },
success: function(response){
//alert (response.redirect);
if (response.redirect) {
window.location.href = response.redirect;
}else {
// Process the expected results...
}
}
})
这是php。
$arr = array ('redirect'=>true,'redirect_url'=>'https://mypage.de/no-
access/');
echo json_encode($arr);
希望这对您有所帮助:
jQuery.ajax({
type: "POST",
url: "../custom_scripts/anmeldung_zurueckziehen.php",
data: { anmeldung_id: anmeldung_id },
success: function(response){
response = JSON.parse(response);
if (response.redirect) {
window.location.href = response.redirect_url;
}else {
// Process the expected results...
}
}
})
您缺少 dataType
属性 ajax
来解码响应:
jQuery.ajax({
type: "POST",
dataType: "json",
url: "../custom_scripts/anmeldung_zurueckziehen.php",
data: { anmeldung_id: anmeldung_id },
success: function(response) {
//console.log(response.redirect);
if (response.redirect) {
window.location.href = response.redirect_url;
} else {
// Process the expected results...
}
}
})
执行此操作的两种方法
第一种方法是 - 在 Php 中使用 JSON headers,如下所示
header('Content-Type: application/json');
上面的 return JSON 字符串与 json headers 和 jquery 将自动为您解析它并且您的回调函数将有适当的 object.
第二种方法是 - 在 javascript 中使用 JSON.parse 如下所示
JSON.parse(response);
以上基本上会将您的 json 字符串解析为 Javascript object
当你从后端设置 JSON headers 时,确保你不解析。
我有以下 ajax 和 php。我想根据我使用 php 脚本输出的结果将用户重定向到另一个页面。 Php 成功 returns json 到浏览器,但我无法将响应 url 返回到 js ajax 以进行重定向。它总是将我重定向到 /undefined。我的代码有什么问题?
jQuery.ajax({
type: "POST",
url: "../custom_scripts/anmeldung_zurueckziehen.php",
data: { anmeldung_id: anmeldung_id },
success: function(response){
//alert (response.redirect);
if (response.redirect) {
window.location.href = response.redirect;
}else {
// Process the expected results...
}
}
})
这是php。
$arr = array ('redirect'=>true,'redirect_url'=>'https://mypage.de/no-
access/');
echo json_encode($arr);
希望这对您有所帮助:
jQuery.ajax({
type: "POST",
url: "../custom_scripts/anmeldung_zurueckziehen.php",
data: { anmeldung_id: anmeldung_id },
success: function(response){
response = JSON.parse(response);
if (response.redirect) {
window.location.href = response.redirect_url;
}else {
// Process the expected results...
}
}
})
您缺少 dataType
属性 ajax
来解码响应:
jQuery.ajax({
type: "POST",
dataType: "json",
url: "../custom_scripts/anmeldung_zurueckziehen.php",
data: { anmeldung_id: anmeldung_id },
success: function(response) {
//console.log(response.redirect);
if (response.redirect) {
window.location.href = response.redirect_url;
} else {
// Process the expected results...
}
}
})
执行此操作的两种方法
第一种方法是 - 在 Php 中使用 JSON headers,如下所示
header('Content-Type: application/json');
上面的 return JSON 字符串与 json headers 和 jquery 将自动为您解析它并且您的回调函数将有适当的 object.
第二种方法是 - 在 javascript 中使用 JSON.parse 如下所示
JSON.parse(response);
以上基本上会将您的 json 字符串解析为 Javascript object
当你从后端设置 JSON headers 时,确保你不解析。