【问题标题】:Joomla sending Data (special Image) with Ajax (Formdata)Joomla 使用 Ajax (Formdata) 发送数据(特殊图像)
【发布时间】:2016-12-13 15:17:51
【问题描述】:

使用 Joomla: 我的问题是当我提交按钮时,ajax 将一个空数据数组发送回我的客户端。在控制台中调试显示标题中有数据,但预览和响应值为空。

这是我的代码(我使用的是引导程序中的模态表单)。

我的默认脚本中的 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);
            })  

        });

这里是我的controller.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();
        }

我在控制台日志中得到了数据/变量。

它是: 新消息:空, 图像:空

尝试设置 contentType: false 会给我一个 500 错误。

非常感谢

这是来自我网络的信息 enter image description here

【问题讨论】:

  • form.append("message-image-upload", $("#message-image-upload")[0].files[0]);这是为了获取文件数据
  • 我想通了。

标签: ajax file joomla upload


【解决方案1】:

我想通了。 这是我的 ajax 命令中的 URL。 当我使用像

这样的普通网址时
url: 'upload.php' 

这会起作用,然后我可以设置

contentType: false,

但这还不够安全。 我只想用这个网址

url: "index.php?option=com_addproduct&task=sendMessages&format=json",

但后来我收到了找不到视图的错误消息。这很奇怪。

【讨论】:

  • 好的,我找到了解决方案。我正在使用外部文件(upload.php)并包含 joomla 框架。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 2016-09-09
  • 1970-01-01
  • 2023-03-12
  • 2014-05-28
  • 1970-01-01
  • 2021-04-14
相关资源
最近更新 更多