2017-04-26 31 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