Javascript 为什么jQuery formData需要contentType=false?
我的问题是关于使用jQuery发送表单数据。我了解到,当省略contentType行时,jQuery默认为x-www-formdata-urlencoded编码。这是我认为应该使用的。但如果contentType设置为false,则使用多部分/表单数据编码 然而,当contentType被省略时,PHP表示“searchcity”索引未定义,这意味着jQuery没有从我的HTML输入成功发送“searchcity”。当contentType设置为false时,这意味着jQuery使用多部分/表单数据,PHP能够找到索引,一切正常 但在HTML中,如果省略“enctype”,则默认为x-www-formdata-urlencoding。如果我使用Javascript 为什么jQuery formData需要contentType=false?,javascript,jquery,content-type,form-data,Javascript,Jquery,Content Type,Form Data,我的问题是关于使用jQuery发送表单数据。我了解到,当省略contentType行时,jQuery默认为x-www-formdata-urlencoded编码。这是我认为应该使用的。但如果contentType设置为false,则使用多部分/表单数据编码 然而,当contentType被省略时,PHP表示“searchcity”索引未定义,这意味着jQuery没有从我的HTML输入成功发送“searchcity”。当contentType设置为false时,这意味着jQuery使用多部分/表单
,它使用x-www-formdata-urlencoding,工作正常。我不需要使用
那么,为什么在发送表单时,HTML可以使用x-www-formdata-urlencoding成功地将表单数据发送到PHP,而jQuery需要多部分/表单数据呢
我只发送文本,没有文件
<html>
<header><script src = "/jquery.js"></script></header>
<a href="/login.html">Login/Register</a> <br>
<form id = "searchform">
<input type = "text" name = "searchcity" size = "40"><br>
<input type = "button" id = "submit" value = "Search">
</form>
<script>
$("#submit").click(function(){
if( $("#searchform")[0].reportValidity() ){
var form = new FormData( $("#searchform")[0] );
$.post({
url:"/search.php",
processData:false, //prevents jquery from turning form to string
contentType:false, // this means to use multipart form encoding, but why?
data:form,
success:function(data){
alert(data);
}
});
}
});
</script>
</html>
contentType选项为false用于传递文件的多部分/表单数据表单。如果将contentType选项设置为false,则强制jQuery不添加内容类型标题,否则,边界字符串将丢失。
如果contentType设置为false,则意味着jQuery使用多部分/表单数据。
。不正确,请阅读文档:从jQuery 1.6开始,您可以传递false来告诉jQuery不要设置任何内容类型标题。
(在contentType下)我明白了,但是如果是这样,HTML为什么仍然可以通过x-www-formdata-urlencoding发送表单,但是jQuery需要contentType:false?默认情况下,如果省略contentType,jQuery将通过x-www-formdata-urlencoding发送表单。我明白了,但是如果是这样,为什么HTML仍然可以通过x-www-formdata-urlencoding发送表单,但是jQuery需要一个contentType:false?默认情况下,如果省略contentType,jQuery将通过x-www-formdata-urlencoding发送。
https://pastebin.com/rMh3dr7r