【问题标题】:AjaxForm Submit posting empty valuesAjaxForm 提交发布空值
【发布时间】:2020-11-24 01:55:05
【问题描述】:

我对 AjaxForm Submit 有一个奇怪的问题。我必须调试的项目应该使用 Ajax 将选定的文件上传到服务器 (PHP),以便可以跟踪上传进度。我确保我的 php.ini 允许 POST 和 Upload 大小为 100+ MB,max_execution_time 为 5 分钟。无论如何,如果我上传小于 9 MB(包括 8.9 MB)的文件,一切都很好,文件已上传。 9 MB 是“有时有效,有时无效),超过 10 MB 的所有内容都保证会导致问题。 在下面的代码(短版)中,我将评论导致问题的地方,请让我知道您认为可能是什么:

HTML(省略了进度条等一些东西,使代码易于阅读):

<form action="#" method="post" id="publisher-box-focus">
      ...controls for choosing a file etc....
     <button type="button" onclick="Wo_GetPRecordLink()" id="publisher- 
            button"><span>Share</span></button>
     <input type="hidden" name="hash_id" value="some_generated_id_is_here">
</form>

JS:

function Wo_GetPRecordLink() {
     $('form.post').submit();
}

$('form.post').ajaxForm({
    url: '<url>/myPhp.php',
    beforeSend: function () {
      var percentVal = '0%';
      bar.width(percentVal);
      percent.html(percentVal);
    },
    uploadProgress: function (event, position, total, percentComplete) { ***This works fine
      var percentVal = percentComplete + '%';
      bar.width(percentVal);
      $('#progress').slideDown(200);
      percent.html(percentVal); 
    },
    success: function (data) {
      if(data.status == 200) {
        //My PHP does return 200 after file upload is complete but it gets incomplete POST
             data thus sends no data response i'm expecting!
      } 
    }
  });

PHP:

<?php
$hash_id = '';
if (!empty($_POST['hash_id'])) {
    $hash_id = $_POST['hash_id'];
}
echo "HASH IS: ".$hash_id; //*****HERE when uploading big files (and only files > 9MB 
 $hash_id is empty even thou Chrome Inspector shows that hidden input value is set 
 properly! This happens only for files bigger than 9 MB!
...

大家都遇到过这种问题吗?您知道是什么原因造成的吗?

服务器是 ec2 Amazon 实例。

PHP 版本 5.5.38

【问题讨论】:

  • 您缺少方法类型。 $("#theForm").ajaxForm({url: 'server.php', type: 'post'})
  • 嗯没有帮助:(
  • 在本地环境中一切正常吗?
  • hmmm 我只在网站上部署了这个版本,因为它是一个复杂的系统,我只是要“修复一些错误”

标签: javascript php ajaxform


【解决方案1】:

如果你的服务器设置正确,则ajax post方法有问题。

请不要忘记文件输入名称属性:)

你可以试试这个吗?

HTML:

<form action="#" method="post" id="publisher-box-focus">
  ...controls for choosing a file etc....
 <input type="hidden" name="hash_id" value="some_generated_id_is_here">
 <button type="button" onclick="Wo_GetPRecordLink(event)" id="publisher-button"><span>Share</span></button>
</form>

JS:

function Wo_GetPRecordLink(e) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: '<url>/myPhp.php',
        data: $('#publisher-box-focus').serialize(),
        beforeSend: function () {
            var percentVal = '0%';
            bar.width(percentVal);
            percent.html(percentVal);
        },
        uploadProgress: function (event, position, total, percentComplete) {
            var percentVal = percentComplete + '%';
            bar.width(percentVal);
            $('#progress').slideDown(200);
            percent.html(percentVal);
        },
        success: function (data) {
            if (data.status == 200) {
            
            }
        }
    });
}

【讨论】:

  • 嗯不,还是一样:(
猜你喜欢
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 2012-08-24
  • 2015-11-15
  • 1970-01-01
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多