javasript利用jquery发送请求的各种方法

Jquery发送ajax请求的方法有很多,其中最基本的是$.ajax方法,在其之上封装的方法有 $.get, $post, $.put, $.ajaxForm, $fileUpload等。而在这些上层的方法中,后两个为jquery的插件所提供,如果需要用到的话,还需要引入对象的js库文件。这里我们记录下各方法的使用及ajax方法最原始的调用方式。


$get方法:

$.get(
	'http://www.baidu.com', 
	function(html){
		console.log(html);
	}
);
$.ajax({
	type : "GET",
	url : 'http://www.baidu.com', 
	success : function(html){
				console.log(html);
			  }
});

$.post方法:

$.post(
	'http://www.baidu.com/search', 
	{query : "javascript"},
	function(data){
		console.log(data);
	}
);
$.ajax({
	type : "POST",
	url : 'http://www.baidu.com/search', 
	data : {query : "javascript"},
	contentType : "application/x-www-form-urlencoded",
	success : function(data){
				console.log(data);
			  }
});

//提交存json数据
$.ajax({
	type : "POST",
	url : 'http://www.baidu.com/search', 
	data : {query : "javascript"},
	contentType : "application/json",
	success : function(data){
				console.log(data);
			  }
});


//提交from数据
$.ajax({
	type : "POST",
	url : 'http://www.baidu.com/search', 
	data : $("form").serialize(),    //不带文件的form表单
	success : function(data){
				console.log(data);
			  }
});

$.ajaxFrom方法:

$('form').ajaxFrom({
	target : '#result',
	beforeSubmit : function(formData, jqForm, options){
		console.log(options);
	},
	success : function(responseText, statusText){
		console.log(responseText);
	}
});


$.ajax({      
	type: 'POST', 
	url: '/upload',    
	data: new FormData($('from')[0]),    //带文件的form表单
	contentType: false,    
	processData: false,    
	success: function (result) {    
		console.log(result);    
	},    
	error: function (err) {    
		console.log(err);    
	}    
}); 

$.fileupload方法:

$('#fileupload').fileupload({
	dataType: 'json',
	done: function (e, data) {
		console.log(JSON.stringify(data));
	}
});

var fd = new FormData();
fd.append('file', $('#fileupload')[0].files[0]); 
fd.append('file2', new File([fileBlob], 'filename.txt')); 
$.ajax({      
	type: 'POST', 
	url: '/upload',    
	data: fd,    
	contentType: false,    
	processData: false,    
	success: function (result) {    
		console.log(result);    
	},    
	error: function (err) {    
		console.log(err);    
	}    
}); 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上帝De助手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值