[英]Ajax call to controller, on success redirect to another page an display data return from Ajax call
I am trying to call an ActionMethod
inside the controller
and if data has been successfully returned, I want to redirect the user to a new page and display the data within a div
on the new page! 我试图在
controller
内调用ActionMethod
,如果成功返回了数据,我想将用户重定向到新页面并在新页面上的div
显示数据! Obviously when data is returned, the div
still has not been loaded and therefore I cannot get the data displayed. 显然,当返回数据时,
div
仍未加载,因此无法获取显示的数据。
Any other way to do it? 还有其他方法吗?
test1: function () {
var type = $("#type1").val();
$.ajax({
url: '/testcontroller/method1',
type: "POST",
data: { type: type }
})
.always(function () {
})
.done(function (data) {
console.log("finished");
xxx.xxxxxx.test2();
})
.fail(function (data) {
console.log("failed");
})
},
test2: function() {
var type = $("#type1").val();
var vm =
{
type: type
}
$.ajax({
url: '/testcontroller/method2',
type: "POST",
data: { request:vm }
})
.always(function () {
console.log("always");
})
.done(function (data) {
console.log("done");
window.location.replace("/testpage");
jQuery(window).load(function () {
$("#testdiv").replaceWith(data);
});
})
.fail(function (data) {
console.log("failed");
})
},
Also both functions have a racing condition and sometimes they make conflict with each other, but even if I separate them, the first question remains. 同样,两个功能都有竞争的条件,有时它们会相互冲突,但是即使我将它们分开,第一个问题仍然存在。
Do this: 做这个:
// page 1
<form action="/testpage" method="POST" id="myForm">
<input type="hidden" name="old_data" id="old_data">
</form>
<script>
.done(function (data) {
console.log("done");
$('#old_data').val(data);
$('#myForm').submit();
})
</script>
// testpage
if(isset($_POST['old_data'])) {
echo '<div id="testdiv">'.$_POST['old_data'].'</div>';
}
Yes, Chetan and Chris were right. 是的,Chetan和Chris是正确的。 You loose all data being returned by
Ajax
on page reload. 您会丢失
Ajax
在页面重新加载时返回的所有数据。 I got around this by storing request parameters in Session
and called it again from the new page. 我通过在
Session
存储请求参数来解决这个问题,并在新页面中再次调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.