Javascript 通过ajax发送后如何访问另一页中的数组

Javascript 通过ajax发送后如何访问另一页中的数组,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个数组student。我需要通过POST而不是从GET在另一个php页面中传递这个数组,因为它可以包含数千个字符 我试图打开新的页面sheet.php并回显数组student,我只是简单地检查了回显$\u POST['mnu'],但它显示了未定义的索引错误 var http = null; if(window.XMLHttpRequest){ http = new XMLHttpRequest(); } else{ http = new ActiveXObject('Mic

我有一个数组
student
。我需要通过
POST
而不是从
GET
在另一个php页面中传递这个数组,因为它可以包含数千个字符

我试图打开新的页面
sheet.php
并回显数组
student
,我只是简单地检查了回显
$\u 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));
更改http.send('mnu='+JSON.stringify(student))to
http.send(JSON.stringify(student))

然后在你的
sheet.php
中使用
json\u decode($\u POST)
获取你的POST数据Change
http.send('mnu='+json.stringify(student))
to
http.send(JSON.stringify(student))


然后在你的
sheet.php
中使用
json\u decode($\u POST)
获取你的POST数据

就像@RamRaider评论的那样。。您向
sheet.php
发出了两个请求。第一个是“静默”POST请求,第二个是在第一个POST请求成功完成后的GET请求

第二个请求不会共享第一个请求的有效负载

如果我理解正确,下面的代码应该做你想要的

// 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);
//创建表单元素
// 
var tempForm=document.createElement('form');
setAttribute('action','sheet.php');
setAttribute('method','POST');
tempForm.setAttribute('target','u blank');//新标签中打开
//创建一个输入字段
// 
var tempInput=document.createElement('input');
setAttribute('name','mnu');
tempInput.setAttribute('value',JSON.stringify(student));//设置字段值
//将输入添加到表单中
appendChild(tempInput);
//将表单添加到正文以便发布
document.body.appendChild(tempForm);
//提交表格
tempForm.submit();
//删除表单
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();

$(“”{
动作:“sheet.php”,
方法:“POST”,
目标:“\u blank”,
html:$(''){
名称:‘mnu’,
值:JSON.stringify(学生)
}).prop('outerHTML')
}).appendTo($('body')).submit().remove();

像@RamRaider评论的那样。。您向
sheet.php
发出了两个请求。第一个是“静默”POST请求,第二个是在第一个POST请求成功完成后的GET请求

第二个请求不会共享第一个请求的有效负载

如果我理解正确,下面的代码应该做你想要的

// 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);
//创建表单元素
// 
var tempForm=document.createElement('form');
setAttribute('action','sheet.php');
setAttribute('method','POST');
tempForm.setAttribute('target','u blank');//新标签中打开
//创建一个输入字段
// 
var tempInput=document.createElement('input');
setAttribute('name','mnu');
tempInput.setAttribute('value',JSON.stringify(student));//设置字段值
//将输入添加到表单中
appendChild(tempInput);
//将表单添加到正文以便发布
document.body.appendChild(tempForm);
//提交表格
tempForm.submit();
//删除表单
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();

$(“”{
动作:“sheet.php”,
方法:“POST”,
目标:“\u blank”,
html:$(''){
名称:‘mnu’,
值:JSON.stringify(学生)
}).prop('outerHTML')
}).appendTo($('body')).submit().remove();

当然,上面的代码将有效地向
sheet.php发出两个请求-一个POST请求,然后在成功后发出GET请求?如果您使用jquery,只需在不使用上述代码的情况下使用它即可
$.POST('sheet.php',{mnu:student},function(){window.open('sheet.php','u blank')
@Talal,我尝试使用
$.post
,它重定向,但同样的问题
未定义索引$\u post['mnu']
。有没有其他途径可以访问??我只是在做
if(isset($\u POST['mnu']){echo$\u POST['mnu'];}
dude,ajax请求会在请求成功后发布数据,它会打开
sheet.php
,而这一次没有数据发布。当然,上述请求会有效地向
sheet.php
-发布请求,然后,当成功时,GET请求?如果您使用的是jquery,只需在不使用上述代码的情况下使用它即可。$.post('sheet.php',{mnu:student},function(){window.open('sheet.php','u blank')
@Talal,我尝试使用
$.post
,它重定向,但同样的问题
未定义索引$\u post['mnu']
。有没有其他途径可以访问??我只是在做
if(isset($\u POST['mnu']){echo$\u POST['mnu'];}
dude ajax请求将在请求成功后发布数据,它将打开
sheet.php
,而这一次在php文件中没有数据postedwhat
decode
json\u decode()
需要1个参数,而且它不
student
array
http.send(JSON.stringify(student))
表示将JSON对象转换为字符串。一旦发布到页面
sheet.php
,您需要将其返回到一个对象,这样您就可以使用类似
$student=json\u decode($\u POST)
的东西。然后您应该能够直接作为对象访问您的
student
(例如
$student->name
)php文件中的
解码内容??,
json_decode()
需要1个参数,而不是
student
数组
http.send(json.stringify(student))
表示将JSON对象转换为字符串。一旦发布到页面
sheet.php
,您需要将其返回到一个对象,这样您就可以使用类似
$student=json\u decode($\u POST)
的东西。然后,您应该能够直接作为对象(例如
$student->na)访问您的
student