【1】Ajax
【1.1】Ajax使用
【1.2】编码格式
【1.3】Ajax上传文件
<input type='file' name='my_file' id="files">
<button id="id_btn">提交</button>
$('#id_btn').click(function(){
var formdata = new FormData()
formdata.append('files',$('#files')[0].files[0])
$.ajax({
url:'',
type:'post',
data:formdata,
contentType:false, //告诉jQuery不要去处理发送的数据
processData:false, // 告诉jQuery不要去设置Content-Type请求头
success:function(){
}
})
})
'''
Ajax上传文件格式的数据必须使用FormData函数,因为文件上传的编码格式为form-data
contentType设置为false
processData设置为false
'''
【1.4】Ajax上传json格式的数据
$.ajax({
url: '/demo01/',
method: 'post',
contentType: 'application/json',
data: JSON.stringify({name: 'lqz', age: 19}), // 把对象转成字符串形式,json格式字符串
success: function (data) {
console.log(data)
}
})
【1.5】Ajax执行流程图
