Javascript jQuery在传递POST数据时在新选项卡中打开页面
我有一个名为“list”的javascript变量。我需要将其作为帖子数据发送到另一个页面,并在新选项卡中打开该页面(帖子数据存在) 此代码:Javascript jQuery在传递POST数据时在新选项卡中打开页面,javascript,php,jquery,Javascript,Php,Jquery,我有一个名为“list”的javascript变量。我需要将其作为帖子数据发送到另一个页面,并在新选项卡中打开该页面(帖子数据存在) 此代码: jQuery.post('datadestination.php', list); 发送数据是正确的,但它当然会在同一个选项卡中打开页面 我看到了一些类似问题的解决方案,使用了无形的形式和类似的东西,但我无法让它们发挥作用。有什么简单的解决方案吗?1)为什么不使用jquery.post()函数传递该列表,并将其保存在会话数组中 2) 然后使用windo
jQuery.post('datadestination.php', list);
发送数据是正确的,但它当然会在同一个选项卡中打开页面
我看到了一些类似问题的解决方案,使用了无形的形式和类似的东西,但我无法让它们发挥作用。有什么简单的解决方案吗?1)为什么不使用jquery.post()
函数传递该列表,并将其保存在会话
数组中
2) 然后使用window.open()
函数打开具有相同文件/地址/url的新选项卡
3) 从会话
数组中检索保存的数据
似乎是一种简单明了的方式?您可以使用target=“\u blank”属性发送表单
<form action="datadestination.php" method="POST" target="_blank" id="myform">
<input type="hidden" name="list" id="list-data"/>
<input type="submit" value="Submit">
</form>
这是Sergey解决方案的实现
<?php // this is save.php
session_start();
// DO NOT just copy from _POST to _SESSION,
// as it could allow a malicious user to override security.
// Use a disposable variable key, such as "data" here.
// So even if someone passed _POST[isAdmin]=true, all that he would do
// is populate _SESSION[data][isAuthenticated], which nobody reads,
// not the all-important _SESSION[isAuthenticated] key.
if (array_key_exists('data', $_POST)) {
$_SESSION['data'] = $_POST['data'];
$_SESSION['data.timestamp'] = time();
// Let us let the client know what happened
$msg = 'OK';
} else {
$msg = 'No data was supplied';
}
Header('Content-Type: application/json; charset=utf8');
die(json_encode(array('status' => $msg)));
?>
并在datadestination.php中添加修复程序:
if (!array_key_exists('data', $_SESSION)) {
die("Problems? Did you perchance attempt to reload the page and resubmit?");
// For if he did, then yes, $_SESSION would have been cleared.
// Same if he is operating on more than one window or browser tab.
}
// Do something to validate data. For example we can use data.timestamp
// to assure data isn't stale.
$age = time();
if (array_key_exists($ts = 'data.timestamp', $_SESSION)) {
$age -= $_SESSION[$ts];
}
if ($age > 3600) {
die("Data is more than one hour old. Did someone change server time?!?");
// I actually had ${PFY} do that to me using NTP + --hctosys, once.
// My own time zone is (most of the year) exactly one hour past GMT.
}
// This is safe (we move unsecurity-ward):
$_POST = $_SESSION['data'];
unset($_SESSION['data'], $_SESSION['data.timestamp']);
// keep things clean.
// From here on, the script behaves "as if" it got a _POST.
更新
实际上,您可以合并save.php
和datadestination.php
,并使用“saving stub”savepost.php
,您可以在其他页面中循环使用:
<?php
session_start();
// DO NOT just copy from _POST to _SESSION,
// as it could allow a malicious user to override security.
// Use a disposable variable key, such as "data" here.
if (array_key_exists('data', $_POST)) {
// Timestamp sent by AJAX
if (array_key_exists('ts', $_POST)) {
// TODO: verify ts, but beware of time zones!
$_SESSION['data'] = $_POST['data'];
Header("Content-Type: application/json;charset=UTF-8");
die(json_encode(array('status' => 'OK')));
}
die("Error");
}
// This is safe (we move unsecurity-ward):
$_POST = $_SESSION['data'];
unset($_SESSION['data']); // keep things clean.
?>
并在datadestination.php
(或其他任何地方)中添加
中描述的解决方案似乎有效。你对此有什么具体问题吗?发生了什么?您还可以调用一个javascript函数,动态创建一个带有
target=''blank'
属性的表单:这是一个非常出色的函数。。。不知怎的,我不能忍受这种无形的东西。太好了!我会自己写的,但是对于其他遇到同样问题的人来说,看到有效的解决方案是件好事。啊,我忘了你想在一个新的选项卡中打开它。修正。它会打开一个额外的空白标签。知道原因吗?
if (!array_key_exists('data', $_SESSION)) {
die("Problems? Did you perchance attempt to reload the page and resubmit?");
// For if he did, then yes, $_SESSION would have been cleared.
// Same if he is operating on more than one window or browser tab.
}
// Do something to validate data. For example we can use data.timestamp
// to assure data isn't stale.
$age = time();
if (array_key_exists($ts = 'data.timestamp', $_SESSION)) {
$age -= $_SESSION[$ts];
}
if ($age > 3600) {
die("Data is more than one hour old. Did someone change server time?!?");
// I actually had ${PFY} do that to me using NTP + --hctosys, once.
// My own time zone is (most of the year) exactly one hour past GMT.
}
// This is safe (we move unsecurity-ward):
$_POST = $_SESSION['data'];
unset($_SESSION['data'], $_SESSION['data.timestamp']);
// keep things clean.
// From here on, the script behaves "as if" it got a _POST.
<?php
session_start();
// DO NOT just copy from _POST to _SESSION,
// as it could allow a malicious user to override security.
// Use a disposable variable key, such as "data" here.
if (array_key_exists('data', $_POST)) {
// Timestamp sent by AJAX
if (array_key_exists('ts', $_POST)) {
// TODO: verify ts, but beware of time zones!
$_SESSION['data'] = $_POST['data'];
Header("Content-Type: application/json;charset=UTF-8");
die(json_encode(array('status' => 'OK')));
}
die("Error");
}
// This is safe (we move unsecurity-ward):
$_POST = $_SESSION['data'];
unset($_SESSION['data']); // keep things clean.
?>
$.post('datadestination.php', { data: list, ts: Date.now() }, function(){
window.open('datadestination.php');
});
require 'savepost.php';