我是前端开发的新手,并且面临着无法解决的问题。
我想做的是通过从我的vue.js文件中调用axios.post
方法来打开一个带有参数的新弹出HTML页面(可以将其作为数组传递)。
我搜索了可能的解决方案,但仍然找不到问题的根源。观察Chrome控制台,我怀疑axios.post
可以正常工作,因为它说:
data:“ Array([index_id] => 1 [log_id] => 63)... ow.print(); // });“,状态:200,statusText:” OK“,标题: {…},配置:{…},...}
但是,在弹出窗口中,我无法访问变量,或者它们是null
。
以下代码是我的vue.js中的一个函数:
printTransaction: function(index){
// I have tried this but could not figure out
// var formData = new FormData();
// formData.append('index_id', index);
//
// axios.post('/popup/popup_output_print.php', {
// index_id: index,
// })
// .then(function(response){
// console.info(response);
// })
// .catch(function(error) {
// console.error(error);
// })
// and this too :(
const params = {
index_id: index,
log_id: logId,
};
axios.post('/popup/popup_output_print.php', params, {
headers: {
'content-type': 'application/json',
},
})
.then(function(response) {
console.info(response);
})
.catch(function(error) {
console.error(error);
})
let newWin = window.open('/popup/popup_output_print.php');
setTimeout(function() {
newWin.print();
//newWin.close();
}, 2000);
}
这是 popup_output_print.php
的第一部分<?php require("../_/inc/init.php");
$data = json_decode(file_get_contents("php://input"), TRUE);
$index_id = $data['index_id'];
$log_id = $data['log_id'];
print_r($data);
?>
...
//Trying to print the value
<?php
if($index_id == null) {
echo "index_id is null";
}
else {
echo $index_id;
}
?>
然后弹出窗口将打印index_id is null
我在做什么错了?
更新:在评论之后,我刚刚尝试了var_dump
,并且变量都是null
。
:(
答案 0 :(得分:0)
发生的事情是,axios.post
对window.open完全无效:
let newWin = window.open('/popup/popup_output_print.php');
这会发出没有参数的GET请求。
我认为,更好的方法是使用Vue modal之类的东西,即:
axios(...).then( response => openVueModal(response.data))
如果您更喜欢window.open
选项,则可以将变量作为GET参数传递:
let newWin = window.open(`/popup/popup_output_print.php?index_id=${index}&log_id=${logId}`);
然后,您需要更改后端,以从$_GET
读取var。
其他解决方案可能是将axios.post
放入窗口打开模板中,以便在打开后请求所需的变量。