我有一個名爲「list」的javascript變量。我需要將其作爲POST數據發送到其他頁面,並在新標籤頁(POST數據存在)中打開該頁面。在傳遞POST數據的同時,jQuery在新選項卡中打開頁面
此代碼:
jQuery.post('datadestination.php', list);
發送數據的所有權利,但ofcourse它會打開在同一個標籤頁。
我看到了一些類似問題的解決方案,使用不可見的形式和類似的東西,但我無法讓他們工作。有沒有簡單的解決方案?
我有一個名爲「list」的javascript變量。我需要將其作爲POST數據發送到其他頁面,並在新標籤頁(POST數據存在)中打開該頁面。在傳遞POST數據的同時,jQuery在新選項卡中打開頁面
此代碼:
jQuery.post('datadestination.php', list);
發送數據的所有權利,但ofcourse它會打開在同一個標籤頁。
我看到了一些類似問題的解決方案,使用不可見的形式和類似的東西,但我無法讓他們工作。有沒有簡單的解決方案?
1)爲什麼不將該列表與jquery.post()
函數一起傳遞並保存在SESSION
數組中;
2)然後用window.open()
函數打開一個具有相同文件/地址/ url的新選項卡;
3)從SESSION
數組中檢索保存的數據;
似乎是一個簡單而乾淨的方式?
這是相當明智的......不知何故,我無法與這種看不見的形式的東西裸露在一起。 – zorza
您可以使用target =「_ 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>
然後在JS:
jQuery('#list-data').val(list);
jQuery('#myform').submit();
它打開一個額外的空白標籤任何想法可能是什麼原因? –
這是謝爾蓋的解決方案的實現。
<?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)));
?>
在第一頁:
$.post('save.php', { data: list }, function(response){
if (!response.status) {
alert("Error calling save");
return;
}
if (response.status !== 'OK') {
alert(response.status);
return;
}
// We had a response and it was "OK". We're good.
window.open('datadestination.php');
});
而且在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
和使用「保存存根」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.
?>
現在您的通話變得
$.post('datadestination.php', { data: list, ts: Date.now() }, function(){
window.open('datadestination.php');
});
,並在您的datadestination.php
(或其他地方)添加
require 'savepost.php';
在HTTP描述的解決方案: //stackoverflow.com/questions/7024040/jquery-open-page-in-a-tab-and-pass-some-post-values似乎工作。你有什麼具體問題嗎?怎麼了? – LSerni
你也可以調用一個javascript函數,動態地用'target ='_ blank''屬性創建一個表單:http://stackoverflow.com/questions/7013109/submit-is-not-a-function-error-in- firefox-in-dynamic-created-form-without – Stefan