通过 AJAX 上传文件会导致大量 Undefied Index 错误

Uploading file via AJAX results in a flood of Undefied Index errors

我有一个项目涉及将文档作为表单的一部分上传到 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 中,它 returns 一连串的错误:

[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 header 以设置边界会消除第一个错误(根据 ),但其他字段的 none 会通过。有什么想法吗?

不要明确设置内容类型,即删除行

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

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