【问题标题】:Uploading file via AJAX results in a flood of Undefied Index errors通过 AJAX 上传文件会导致大量未定义索引错误
【发布时间】:2017-07-10 23:28:55
【问题描述】:

我有一个项目涉及在 PHP 中将文档作为表单的一部分上传:

function uploadFlight(oForm, cFunction)
{
    // This tries a number of different methods for getting an AJAX object
    // to ensure cross-browser compatibility.
    var ajax=gimmeAjax(); 
    if (ajax == null) return;

    ajax.function=cFunction;
    ajax.oForm=oForm;

    // Disable all inputs
    cItems=oForm.getElementsByTagName('input');
    iItems=cItems.length;
    for (var i = 0; i < iItems; i++)
    {
        cItems[i].disabled=true;
    }

    // ... and selects.
    cItems=oForm.getElementsByTagName('select');
    iItems=cItems.length;
    for (var i = 0; i < iItems; i++)
    {
        cItems[i].disabled=true;
    }

    ajax.onreadystatechange = function()
    {
        // Redacted as not relevant to this question
    }

    var formData = new FormData();

    for (var i = 0; i < oForm.length; i++)
    {
        oElem=oForm.item(i);
        if (oElem.tagName.toLowerCase() == "input" || oElem.tagName.toLowerCase() == "select")
        {
            switch (oElem.name)
            {
                case 'document':
                    formData.append('document', oElem.files[0]);
                    break;
                case 'project':
                case 'flight_direction':
                case 'flight_type':
                    formData.append(oElem.name, oElem.options[oElem.selectedIndex].value);
                    break;
                case 'employee_id[]':
                    formData.append('employee_id[]', oElem.value);
                    break;
                case 'task':
                    if (oElem.submitted == true) formData.append(oElem.name, oElem.value);
                    break;
                default:
                    formData.append(oElem.name, oElem.value);
            }
        }
    }

    ajax.open("POST", "comms_ajax.php?module=flight&ts="+get_epoch(), true);
    ajax.setRequestHeader("Content-type", "multipart/form-data");
    ajax.send(formData);

    return false;
}

在 IE11 和 Chrome 中,这很好用。在 Edge 中,它返回一连串错误:

[11-Jul-2017 09:18:31 Australia/Sydney] PHP Warning:  Missing boundary in multipart/form-data POST data in Unknown on line 0
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_date in [REDACTED]\comms_ajax.php on line 37
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_id in [REDACTED]\comms_ajax.php on line 63
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_id in [REDACTED]\comms_ajax.php on line 70
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_id in [REDACTED]\comms_ajax.php on line 77
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_id in [REDACTED]\comms_ajax.php on line 111
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: employee_id in [REDACTED]\comms_ajax.php on line 118
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Warning:  str_repeat(): Second argument has to be greater than or equal to 0 in [REDACTED]\comms_ajax.php on line 118
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_id in [REDACTED]\comms_ajax.php on line 120
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: employee_id in [REDACTED]\comms_ajax.php on line 120
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Warning:  array_merge(): Argument #2 is not an array in [REDACTED]\comms_ajax.php on line 120
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: carrier in [REDACTED]\comms_ajax.php on line 139
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_no in [REDACTED]\comms_ajax.php on line 139
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: depart_time in [REDACTED]\comms_ajax.php on line 140
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: arrive_time in [REDACTED]\comms_ajax.php on line 140
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: checkin_time in [REDACTED]\comms_ajax.php on line 141
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_type in [REDACTED]\comms_ajax.php on line 141
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: notes in [REDACTED]\comms_ajax.php on line 142
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_direction in [REDACTED]\comms_ajax.php on line 142
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: airport in [REDACTED]\comms_ajax.php on line 143
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: project in [REDACTED]\comms_ajax.php on line 144
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_id in [REDACTED]\comms_ajax.php on line 144
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: carrier in [REDACTED]\comms_ajax.php on line 156
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_no in [REDACTED]\comms_ajax.php on line 156
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: depart_time in [REDACTED]\comms_ajax.php on line 156
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: arrive_time in [REDACTED]\comms_ajax.php on line 156
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: checkin_time in [REDACTED]\comms_ajax.php on line 156
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_type in [REDACTED]\comms_ajax.php on line 157
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: notes in [REDACTED]\comms_ajax.php on line 157
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: flight_direction in [REDACTED]\comms_ajax.php on line 157
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: airport in [REDACTED]\comms_ajax.php on line 157
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: project in [REDACTED]\comms_ajax.php on line 157
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: task in [REDACTED]\comms_ajax.php on line 161
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: task in [REDACTED]\comms_ajax.php on line 205
[11-Jul-2017 09:18:32 Australia/Sydney] PHP Notice:  Undefined index: task in [REDACTED]\comms_ajax.php on line 224

更改 Content-type 标头以设置边界会消除第一个错误(根据 https://stackoverflow.com/a/40561831/1817255),但其他字段均未通过。有什么想法吗?

【问题讨论】:

  • 顺便说一句,您可以将oElem.options[oElem.selectedIndex].value 简化为oElem.value
  • developer.mozilla.org/en-US/docs/Web/API/FormData 说 Edge 支持FormData,所以看起来应该可以。
  • 当您打开开发者工具并查看 AJAX 请求中发送的内容时,是否表明它正在发送所有这些参数?
  • 尝试将 console.log() 调用添加到添加到 FormData 对象的 for 循环中,以确保它正确地找到输入。
  • @Barmar 感谢您的输入,我可以在“网络”选项卡中看到参数。我从其他有此问题的人那里得到的要点是,当您使用 setRequestHeaders 设置 multipart/form-data 内容类型时,它不会添加边界参数。添加一个不会改变使用的边界文本,它仍然会生成自己的。

标签: php ajax multipartform-data microsoft-edge


【解决方案1】:

不要显式设置内容类型,即删除该行

ajax.setRequestHeader("Content-type", "multipart/form-data");

XMLHttpRequest.prototype.send() 的参数是FormData 对象时,这是默认设置,并且浏览器会自动提供与其正在使用的匹配的boundary 选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-24
    • 1970-01-01
    • 2014-02-11
    • 2020-02-07
    • 1970-01-01
    • 2010-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多