注意:通过$.ajax发送FormData请求时,需要指定两个属性,不设置会导致报错
contentType:false; //不指定请求体内容类型
processData: false; //不进行数据处理操作
<body>
<form id="form">
<input type="text" name="username">
<input type="file" name="file">
<input type="button" value="按钮" id="btn">
</form>
</body>
<script>
$(‘#btn‘).on(‘click‘, function(){
var fd = new FormData($(‘form‘)[0])
$.ajax({
method: ‘post‘,
url: ‘‘,
data: fd,
contentType: false,
processData: false,
success: function(res){
console.log(‘res‘)
}
});
})
jsonp和ajax没有任何关联,jQuery为了我们请求操作时的统一,将jsonp的发送方式设置在了$.ajax中
$(‘#btn‘).on(‘click‘, function () {
$.ajax({
method : ‘POST‘
url : ‘http://localhost/Ajax_day4/demo1.php‘,
success : function (datas) {
console.log(datas);
},
// 如果在dataType中设置‘jsonp‘,表示当前请求以jsonp形式发送,与dataType的原始功能无关
dataType : ‘jsonp‘
});
}
3. ajax设置请求头
$.ajax({
type: type,
headers: {
‘token‘:‘token_value‘
},
url: url,
data: data,
success: function(data) {
},
error: function(err) {
},
complete: function(XMLHttpRequest, status) { //请求完成后最终执行参数
}
});
原文:https://www.cnblogs.com/xhrr/p/ajax.html