亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過ajax發送后如何訪問另一個頁面中的數組

通過ajax發送后如何訪問另一個頁面中的數組

PHP
白衣非少年 2021-04-05 13:15:31
我有一個數組student。我需要通過POST而不是從GET,在另一個php頁面中傳遞此數組,因為它可以包含數千個字符。我試圖打開新頁面sheet.php并回顯數組student,我只是簡單地檢查echo $_POST['mnu'],但是它顯示未定義的索引錯誤。var http = null;if(window.XMLHttpRequest){    http = new XMLHttpRequest();}else{    http = new ActiveXObject('Microsoft.XMLHTTP');}http.open('POST','sheet.php',true);http.setRequestHeader('Content-type','application/x-www-form-urlencoded');http.onreadystatechange = function(){    if(http.readyState==4 && http.status==200){        window.open('sheet.php','_blank')    }}http.send('mnu='+JSON.stringify(student));
查看完整描述

2 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您向發出兩個請求sheet.php。第一個是“靜默” POST請求,第二個是在成功完成第一個POST請求之后的GET請求。

http://img1.sycdn.imooc.com//608272d10001e8ec11930523.jpg

第二個請求將不會共享第一個請求的有效負載。

如果我正確地理解以下代碼,那么您應該做的是...

// Create a form element

// <form action="sheet.php" method="post"></form>

var tempForm = document.createElement('form');

tempForm.setAttribute('action', 'sheet.php');

tempForm.setAttribute('method', 'POST');

tempForm.setAttribute('target', '_blank'); // Open in new tab


// Create an input field

// <input name="mnu" value="...">

var tempInput = document.createElement('input');

tempInput.setAttribute('name', 'mnu');

tempInput.setAttribute('value', JSON.stringify(student)); // Set field value


// Add the input to the form

tempForm.appendChild(tempInput);



// Add the form to the body in order to post

document.body.appendChild(tempForm);


// Submit the form

tempForm.submit();


// Remove the form

document.body.removeChild(tempForm);

而且,如果您使用的是jQuery,則可以簡化上面的代碼。


$('<form>', {

    action: 'sheet.php',

    method: 'POST',

    target: '_blank',

    html: $('<input>', {

        name: 'mnu',

        value: JSON.stringify(student)

    }).prop('outerHTML')

}).appendTo($('body')).submit().remove();


查看完整回答
反對 回復 2021-04-23
?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

更改http.send('mnu='+JSON.stringify(student));http.send(JSON.stringify(student));

然后在您sheet.php使用json_decode($_POST)中獲取您的POST數據


查看完整回答
反對 回復 2021-04-23
  • 2 回答
  • 0 關注
  • 223 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號