![](/img/trans.png)
[英]passing the ajax response data to another page after ajax call on success
[英]Ajax call to controller, on success redirect to another page an display data return from Ajax call
我試圖在controller
內調用ActionMethod
,如果成功返回了數據,我想將用戶重定向到新頁面並在新頁面上的div
顯示數據! 顯然,當返回數據時, div
仍未加載,因此無法獲取顯示的數據。
還有其他方法嗎?
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");
})
},
同樣,兩個功能都有競爭的條件,有時它們會相互沖突,但是即使我將它們分開,第一個問題仍然存在。
做這個:
// 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>';
}
是的,Chetan和Chris是正確的。 您會丟失Ajax
在頁面重新加載時返回的所有數據。 我通過在Session
存儲請求參數來解決這個問題,並在新頁面中再次調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.