使用Joomla:我的问题是,当我提交按钮时,Ajax将空数据阵列发送回我的客户端。控制台中的DebBuging向我展示了标题中的数据,但预览和响应值是空的。
这是我的代码(我正在使用Bootstrap中的模态表单)。
html在我的默认脚本中:
<form action="<?php echo JRoute::_('index.php?option=com_addproduct&view=addproducts'); ?>" method="post" name="modalMessageForm" id="modalMessageForm" enctype="multipart/form-data">
<input type="file" id="message-image-upload" accept="image/*" style="display:none;" name="message-image-upload">
<textarea class="form-control message-textarea" id="message-textarea" placeholder="Nachricht..." name="new-message" rows="4"></textarea>
<button type="button" id="button-close-message" class="btn btn-default btn-block btn-message-close" style="display:none; margin-top:5px;"><?=JText::_( 'COM_ADDPRODUCT_MODAL_MESSAGES_CLOSE')?></button>
</form>
jQuery/ajax:
$(document).on("submit", "#modalMessageForm", function(e)
{
var form = $('#modalMessageForm').get(0);
e.preventDefault();
var formData = new FormData(form);
for(var pair of formData.entries()) {
console.log(pair[0]+ ', '+ pair[1]);
}
$.ajax({
crossDomain: true,
type: "POST",
url: "index.php?option=com_addproduct&task=sendMessages&format=json",
data: formData,
dataType: "json",
processData: false
})
.done(function(data, textStatus, jqXHR){
console.log('Message: '+data.new-message+' PicName: '+data.img);
})
});
在这里我的控制器.php:
public function sendMessages()
{
JResponse::setHeader('Content-Type', 'application/json', true);
$app = JFactory::getApplication();
$input = $app->input;
$new-message = $input->getString('new-message', '', 'RAW');
$img = $_FILES['message-image-upload']["name"];
$img = JFile::makeSafe($img);
$results=array(
'new-message' => 'new-message',
'img' => $img
);
echo json_encode($results);
$app->close();
}
我在控制台日志中获得了数据/变量。
是:新信息:null,IMG:null
尝试设置contentType:false会给我一个500错误。
非常感谢
这是我网络的信息在此处输入图像描述
我弄清楚了。它是我的Ajax命令中的URL。当我使用正常URL时
url: 'upload.php'
将起作用,然后我可以设置
contentType: false,
但这还不够安全。我只想使用此URL
url: "index.php?option=com_addproduct&task=sendMessages&format=json",
,但后来我收到了一个错误消息,即找不到视图。这很奇怪。