Ajax常用模板

 

Ajax介绍:

以前前端获取后端服务器传过来的数据,还要再JS中先new一个XmlHttpRequest对象非常麻烦,现在我们引入Jquery,

直接在前端使用Jquery的$.ajax()方法,然后通过function函数的result参数,就可以异步获取后端服务器的数据。

ajax: 异步通信技术的一种实现; 
不刷新页面的情况下,客户端浏览器网页可以跟服务器后天进行数据交换;

 1、触发一个js事件(单击、键盘、change、blur、focus.....)

 2、调用jQuery的ajax方法

  jQuery.post(url, [data], [callback], [type])

 url:发送请求地址。

 data:待发送 Key/value 参数。     

 callback:发送成功时回调函数。

 type:返回内容格式,xml, html, script, json, text, _default。

3、编写服务器的代码,完成ajax需要的功能,并通过io输出数据;

out.print(返回数据字符串);

4、jQuery的ajax方法处理回调函数的业务(callback方法)

<html>
	<head>
		<title>here</title>
		<!-- 引入 jquery -->
		<script type="text/javascript" src="js/jquery-3.6.3.min.js"></script>

		<script type="text/javascript">

			$(function() {
				$("#dep").on("change", getPerson);
			})

			function getPerson() {
				
				// 第一种模板
				$.ajax({
						url:"ajax_info.txt" ,
						type:"get" ,
						data:"name=zs",
						success:function(result, textStatus){ 
							alert("成功");
							console.log(result)
							
						},
						error:function(xhr,errorMessage,e){
							alert("失败");
						}
					});
			    //第二种模板
					$.get(
						"ajax_info.txt" ,
						"name=zs",
				        function(result, textStatus,xhr){ 
							alert("成功!!!!!");
						}
					);
			   //第三种模板
				   $.post(
						"ajax_info.txt" ,  // 目前我们没有搭建后台服务器,并且请求的是 文件形式。此种情况只能使用 get
						"name=zs",
						   function(result, textStatus,xhr){ 
							alert("成功!!!!!");
						}
					);
			  
			  //第四种模板
			        请求 ajax_info.txt ,将返回值 赋值给 persons 元素
			  $("#persons").load(
			  				"ajax_info.txt"
			   );
			  
			  
			}
			
		</script>



	</head>

	<body>
         <select id="dep"></select>
         <div id="persons"></div>
	</body>
</html>

第一种模板:

$.ajax({
                        url:"url请求地址" ,
                        type:"请求方式(“get”,“post”)" ,
                        data:"参数传递",
                        success:function(result, textStatus){ 
                            alert("成功");
                            console.log(result)
                            
                        },//请求成功执行success函数
                        error:function(xhr,errorMessage,e){
                            alert("失败");
                        }//失败执行error函数
                    });

第二种模板:

$.get(
                        "url请求地址",
                        "参数传递",
                        function(result, textStatus,xhr){ 
                            alert("成功!!!!!");
                        }//成功函数
                    );

第三种模板:

  $.post(
                        "ajax_info.txt" ,  // 目前我们没有搭建后台服务器,并且请求的是 文件形式。此种情况只能使用 get
                        "参数传递",
                           function(result, textStatus,xhr){ 
                            alert("成功!!!!!");
                        }//成功函数
                    );

第四种模板:

  $("#persons").load(
                "ajax_info.txt"
               );//  请求 ajax_info.txt ,将返回值 赋值给 persons 元素

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: jQuery 的 ajax 模板如下: ``` $.ajax({ url: "your url", type: "POST/GET/PUT/DELETE", data: { your data }, success: function (response) { // do something on success }, error: function (error) { // do something on error } }); ``` 你可以根据需要修改 type、url、data 等参数。 ### 回答2: 有,jQuery 提供了一个方便的方式来使用 AJAX 技术,包括发送 HTTP 请求并处理服务器响应的功能。可以使用简洁的代码实现异步加载数据、更新页面内容、发送表单数据等操作。 下面是一个使用 jQuery 的 AJAX 模板示例: ```javascript // 发送 GET 请求 $.ajax({ url: 'example.com/api/data', method: 'GET', dataType: 'json', success: function (response) { // 请求成功的处理逻辑 console.log(response); }, error: function (xhr, status, error) { // 请求出错的处理逻辑 console.log(error); } }); // 发送 POST 请求 $.ajax({ url: 'example.com/api/data', method: 'POST', data: { username: 'John', password: '123456' }, success: function (response) { // 请求成功的处理逻辑 console.log(response); }, error: function (xhr, status, error) { // 请求出错的处理逻辑 console.log(error); } }); // 发送表单数据 $('#myForm').submit(function (event) { event.preventDefault(); // 阻止表单默认提交行为 var formData = $(this).serialize(); // 将表单数据序列化 $.ajax({ url: 'example.com/api/data', method: 'POST', data: formData, success: function (response) { // 请求成功的处理逻辑 console.log(response); }, error: function (xhr, status, error) { // 请求出错的处理逻辑 console.log(error); } }); }); ``` 使用这个模板,你可以根据具体需求来设置请求的 URL、请求方法、数据类型、数据等信息,并在 success 回调函数中处理服务器返回的数据,或者在 error 回调函数中处理请求错误。 ### 回答3: 有,jQuery的ajax方法是一种常用的发送HTTP请求并处理响应的方法。它使用简单且灵活,可以通过设置参数来实现不同的功能。以下是一个简单的jQuery的ajax模板: ```javascript $.ajax({ url: "your_url", method: "GET", // 请求方法,可以是GET、POST、PUT等 data: {}, // 发送到服务器的数据,可以是对象、字符串或jQuery序列化的表单数据 dataType: "json", // 期望的响应数据类型,可以是json、xml、html等 success: function(response) { // 请求成功时执行的回调函数,response为服务器返回的数据 console.log(response); }, error: function(xhr, status, error) { // 请求失败时执行的回调函数,xhr为XMLHttpRequest对象,status为错误状态, // error为错误信息 console.error(error); } }); ``` 在实际使用中,你需要根据具体的需求设置url、method、data等参数,然后通过success和error回调函数处理服务器响应。以上仅为一个简单的模板,你可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奋斗着,享受着

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

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

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

打赏作者

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

抵扣说明:

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

余额充值