get
$.get('/index.php',data,function(res){ //请求成功},'json');
post
$.post('/index.php',data,function(res){ //请求成功},'json');
ajax
$.ajax({ type: "POST",//post请求 url: "",//请求地址 data: data,//js对象{} dataType: "json",//返回格式json beforeSend:function(){ //发送请求前 }, complete:function(){ //请求完成 }, success: function(res) { console.log(res); //成功返回 }, error:function(){ //请求失败 },});
ajax_file
var formData = new FormData();formData.append("file",$("#file")[0].files[0]);//添加要上传的文件formData.append("name",name);//添加其他数据$.ajax({ type: "POST",//post请求 url: "",//请求地址 data: formData, dataType: "json",//返回格式json processData : false,// 告诉jQuery不要去处理发送的数据,用于对data参数进行序列化处理 contentType : false, // 告诉jQuery不要去设置Content-Type请求头 beforeSend:function(){ //发送请求前 }, complete:function(){ //请求完成 }, success: function(res) { console.log(res); //成功返回 }, error:function(){ //请求失败 },});
ajax_files
var formData = new FormData();formData.append("file",$("#file")[0].files);//添加要上传的文件,设置上传的form属性enctype = “multipart/form-data”formData.append("other_data",other_data);//添加其他数据$.ajax({ type: "POST",//post请求 url: "",//请求地址 data: formData, dataType: "json",//返回格式json processData : false,// 告诉jQuery不要去处理发送的数据,用于对data参数进行序列化处理 contentType : false, // 告诉jQuery不要去设置Content-Type请求头 beforeSend:function(){ //发送请求前 }, complete:function(){ //请求完成 }, success: function(res) { console.log(res); //成功返回 }, error:function(){ //请求失败 },});