jQuery封装ajax

34 阅读1分钟

jQuery封装ajax

jQuery实现对ajax的封装,使代码轻量化 AJAX—.get().get() .get()是一个简单的 GET 请求功能。请求成功时可调用回调函数。

$.get(url,[data],[callback],[type])

url:发送请求地址。 data:待发送 Key/value 参数。 callback:发送成功时回调函数。 type:返回内容格式,xml, html, script, json, text, _default

注意:请求是通过 URL 提交的 提交有大小限制(2KB)

image.png

html:
	<button onclick="fn()">jQuery-ajax</button>
		<script>
			function fn(){
				// console.log(1111)
				// console.log($)
				var url="http://192.168.6.60:8080/api1"
				// $.get(url,(res)=>{
				// 	console.log(res,111111)
				// })
				
				// $.ajax({
				// 	url:"http://192.168.6.60:8080/api1",
				// 	method:"GET",
				// 	success:function(res){
				// 		console.log(res)
				// 	},
				// 	error:(err)=>{
				// 		console.log(err,111)
				// 	}
				// })
			}
		</script>