2017-04-26 67 views
1

即时通讯尝试从JavaScript发送数组到Joomla 3.x的PHP文件。发送数组从javascript到php通过POST在Joomla 3.x

var options = ['foo', 'bar']; 

$.post('index.php?option=component&view=componentview&Itemid=123&tmpl=component&layout=xlsx', {'xlsx_options': options}) 

然后,我有一个php文件,它创建数据以下载xlsx文件。但我需要通过这个选项数组进行筛选,并且我无法检索它。我试过这个没有成功。

$xlsx = $_POST['xlsx_options']; 
$xlsx = json_decode($_POST['xlsx_options'], true); 
$xlsx = JFactory::getApplication()->input->get('xlsx_options'); 

任何帮助?谢谢!

回答

1
$j.ajaxSetup({ 
    contentType: "application/json; charset=utf-8" 
}); 
var options = ['foo', 'bar']; 
$j.post('index.php?option=component&view=componentview&Itemid=123&tmpl=component&layout=xlsx', JSON.stringify({'xlsx_options': options})); 

我不知道如果不是所有的ajax请求都发送JSON有效负载,您将需要执行ajaxSetup。您可以使用$ .ajax提交您的发布请求并在其中指定dataType(如下所示)

$.ajax({ 
    url: 'index.php?option=component&view=componentview&Itemid=123&tmpl=component&layout=xlsx', 
    type:"POST", 
    data: JSON.stringify({'xlsx_options': options}), 
    contentType:"application/json; charset=utf-8", 
    dataType:"json", 
    success: function(){ 
    ... 
    } 
}) 
+0

请让我知道此解决方案是否适合您。谢谢! – Woodrow