简体   繁体   English

jQuery AJAX 在表单请求中出现文件失败

[英]jQuery AJAX fail with files in form request

I have an issue with jQuery AJAX call on my Laravel application.我在 Laravel 应用程序上调用 jQuery AJAX 时遇到问题。 It fails when using type="file" in the form.在表单中使用type="file"时失败。

I can't reproduce the issue locally, it happens on prod server only.我无法在本地重现该问题,它仅发生在产品服务器上。

$.ajax({
    url: url,
    type: $(this).attr("method"),
    dataType: "JSON",
    data: new FormData(this),
    processData: false,
    contentType: false,
    ...

The error I get is:我得到的错误是:

POST... net::ERR_CONNECTION_CLOSED POST... 网络::ERR_CONNECTION_CLOSED

No errors in Laravel logs, if I dump error in error callback there is only a message "error", so nothing helpful there. Laravel 日志中没有错误,如果我在错误回调中转储错误,则只有一条消息“错误”,所以那里没有任何帮助。

What might be the issue?可能是什么问题?

For some reason the server freaks out if you set file type and try to submit a form without actually submitting the actual file.由于某种原因,如果您设置文件类型并尝试提交表单而不实际提交实际文件,则服务器会崩溃。 In that case the field comes up as having name "" and size 0 .在这种情况下,该字段显示为具有名称""和大小0

Doing this worked like a charm:这样做就像一个魅力:

var verifyIdentity = formData.get('verify_identity')

if(verifyIdentity && verifyIdentity.size === 0){
    formData.delete('verify_identity')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM