我有一个表单,当我提交表单时,它将重定向到我从控制器指定的url (函数控制器作为操作表单)。我已经做了一个函数控制器来处理表单,并且我有几个不同的条件,当我提交表单时可以从json结果重定向。但我希望ajax从json结果重定向到其中一个重定向。
如何从json结果重定向到特定的重定向?
json结果:
{"result":"ok","message":"Failed!!! ","redirect":"http:\/\/localhost\/myproject\/approval\/detail\/21?status=approve_failed"}
{"result":"ok","message":"Success!!!","redirect":"http:\/\/localhost\/myproject\/approval\/detail\/21?status=approve_success"}这是我提交表单时重定向的方式:
var frm = $('#form-process');
frm.submit(function(e) {
e.preventDefault();
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
dataType: "json",
success: function(res) {
if (res.result == "ok") {
if (res.redirect) {
window.location.href = res.redirect;
}
$.notify("Success. Data saved.", "success");
$('#modalDateEst').modal('hide');
table.ajax.reload();
} else {
$.notify("System gone wrong's. Please contact Administrator", "error");
}
},
error: function(data) {
}
});
});发布于 2021-11-02 07:06:31
我已经找到解决方案了。这是我要做的。我在控制器中使用的函数中创建了一个变量$redirect。然后我创造了一些条件。
在条件中应该是:
if (//condition) {
$arred = array(
"result" => "ok",
"message" => "Success!!! ",
'redirect' => base_url(//my url for the redirect)
);
$redirect = $arred;
}所以在我的函数控制器中应该是这样的:
function something() {
$redirect = "";
if (//condition) {
//do something
$arred = array(
"result" => "ok",
"message" => "Success!!! ",
'redirect' => base_url(//my url for the redirect)
);
$redirect = $arred;
}
if (//condition2) {
//do something
$arred = array(
"result" => "ok",
"message" => "Success!!! ",
'redirect' => base_url(//my url for the redirect)
);
$redirect = $arred;
}
//make the json
echo json_encode($redirect);
}注意:此解决方案只会将jquery重定向到填充$redirect变量的最新重定向。所以如果你得到了多重定向,那么唯一使用的重定向就是最新的重定向。在这种情况下,我只需要最新的重定向。这就是我的解决方案。
https://stackoverflow.com/questions/69805700
复制相似问题