Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ajax表单提交重定向与非ajax表单类似_Javascript_Jquery - Fatal编程技术网

Javascript ajax表单提交重定向与非ajax表单类似

Javascript ajax表单提交重定向与非ajax表单类似,javascript,jquery,Javascript,Jquery,我有这个代码,我用它来提交带有附件文件的表单 $("#career_form").submit(function(e){ var this_current = $(this); var formData = new FormData(this_current[0]); var url = this_current.attr("action"); $.ajax({ url : url, data: formData,

我有这个代码,我用它来提交带有附件文件的表单

$("#career_form").submit(function(e){
    var this_current = $(this);
    var formData = new FormData(this_current[0]);
    var url = this_current.attr("action");
    $.ajax({
        url : url,
        data: formData,
        type: 'post',
        cache: false,
        async: true,
        beforeSend: function(){  },                   
        success: function(response){
            if(response === true){
                alert("successfully sent");
            }
        }
    });
    e.preventDefault();
});
但是表单一直将我重定向到它的目标文件“动作中的url”,就像它不是ajax提交一样,但是如果我用

data: $(this).serialize();

它是有效的(ajax提交),有什么想法、帮助、建议、建议吗?

给e.preventDefault();在函数的开头

jQuery在默认情况下尝试将数据转换为查询字符串,但使用
newformdata
时会抛出错误

要将formData用于jquery ajax请求,请使用选项
processData
,并将其设置为false,如:

 $.ajax({
        url : url,
        data: formData,
        type: 'post',
        cache: false,
        async: true,
    processData: false,
        beforeSend: function(){  },                   
        success: function(response){
            if(response === true){
                alert("successfully sent");
            }
        }
    });
这就是为什么它与示例中的
serialize
一起工作,而不与
formData
一起工作的原因


e.preventDefault
工作正常,但如果之前出现错误,它将无法工作。通过将
e.preventDefault
置于函数顶部,无论后续代码中是否存在错误,它都将阻止该操作。

您可以编辑
var formData=new formData(当前[0])并使用以下行:

var formData = new FormData(document.querySelector("#career_form"));
另外,如果您使用多部分表单发送表单中的文件,则需要在ajax调用中设置以下参数

  processData: false,  // tell jQuery not to process the data
  contentType: false   // tell jQuery not to set contentType
希望这有帮助。查看有关使用formData的更多信息请尝试以下操作:

$("#career_form").submit(function(e){
    e.preventDefault();
    var fd = new FormData(document.querySelector("form"));
    fd.append("CustomField", "This is some extra data");
    $.ajax({
        url: "change-status.php",
        type: "POST",
        data: fd,
        processData: false,
        contentType: false,
        success: function(response){
            if(response){
                alert("successfully sent");
            }
        }
    });
});

您的浏览器控制台中是否有任何错误?E's no error,如果有错误,我将无法跟上,因为表单会重定向(action属性中的url,其行为类似于无ajax表单)。在控制台中,选中“保留日志”复选框,然后提交表单,以便在新页面请求时不会删除日志。我接受
Anoop LL
的回答,并选中此复选框。。我想可能是你给的
,给它的形式是
。。。因为几天前我也犯了同样的错误…@phpfresher:我试过他的,但我得到了这个错误,而恰恰是“uncaughttypeerror:非法调用”。默认值();必须是要给出的第一个指令。然后你可以开始用表单数据详细说明其他内容,我得到了这个错误“UncaughtTypeError:非法调用”,上面的帖子是最新的。请参考。give processData:false after cacheany您需要在开始时给出e.preventDefault(),否则表单会自动调用指定的操作。如有任何想法,请帮助如何将其放在ajax函数的数据参数上(如何将附件与其他表单输入一起压缩到新的formData)我做了更多的研究,发现我以前的答案是错的。所以,我更新了我的答案。请仔细看看,并为之前给出的错误答案感到抱歉。