jQuery对Ajax的支持

jQuery对Ajax的支持

在jQuery中提供有$.ajax()方法,该方法有如下常用参数:

No设置参数描述
1urlAjax的执行处理路径
2method表示当前发出的Http请求模式,get或者post
3data向服务器传递的参数集合
4dataType服务器返回数据类型(text、json、xml、html、jsonp、script)
5success处理成功后执行的回调函数
6error请求失败时的处理函数
使用Ajax处理JSON数据

1.编写Servlet程序

package com.ajax.servlet;

import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(urlPatterns = "/test")
public class TestServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        JSONObject obj = new JSONObject();
        Enumeration<String> enu = request.getParameterNames();
        while(enu.hasMoreElements()){
            String paramName = enu.nextElement();
            String paramValue = request.getParameter(paramName);
            obj.put(paramName,paramValue);
        }
        response.getWriter().print(obj.toString());
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}

2.编写页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax</title>
    <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#age").on("change",function(){
                $("#ageMsg").text($("#age").val());
            });
            $("#sendBut").on("click",function(){
                $.ajax({
                    url:'test',
                    method:'post',
                    data:{
                        name:$('#name').val(),
                        age:$('#age').val()
                    },
                    dataType:'json',
                    success:function(data){
                        $('#showDiv').append('<p>姓名:'+data.name+'\t年龄:'+data.age+'</p>');
                    },
                    error:function(){
                        window.console.log('发生了错误!!!');
                    }
                });
            });
        });
    </script>
</head>
<body>
<div id="showDiv"></div>
<div>
    <label>姓名:</label>
    <input type="text" id="name" name="name" placeholder="请输入姓名" autofocus/><br>
    <label>年龄:</label>
    <input type="range" id="age" name="age" min="0" max="100" />
    <span id="ageMsg"></span><br>
    <input type="button" id="sendBut" value="发送" />
</div>
</body>
</html>

运行效果如下所示:

分别处理get与post请求

为了帮助用户简化代码,jQuery还提供有两个简短的函数:

  • 处理GET请求:$.get(请求地址,{参数:内容...},回调函数,'json|text|xml'};
  • 处理POST请求:$.post(请求地址,{参数:内容...},回调函数,'json|text|xml'};
    对以上页面操作,可以用一下程序简化:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax</title>
    <script type="text/javascript" src="js/jquery-3.4.1.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#age").on("change",function(){
                $("#ageMsg").text($("#age").val());
            });
            $("#sendBut").on("click",function(){
                $.post('test',{
                    name:$('#name').val(),
                    age:$('#age').val()
                },function(data){
                    $('#showDiv').append('<p>姓名:'+data.name+'\t年龄:'+data.age+'</p>');
                },'json');
            });
        });
    </script>
</head>
<body>
<div id="showDiv"></div>
<div>
    <label>姓名:</label>
    <input type="text" id="name" name="name" placeholder="请输入姓名" autofocus/><br>
    <label>年龄:</label>
    <input type="range" id="age" name="age" min="0" max="100" />
    <span id="ageMsg"></span><br>
    <input type="button" id="sendBut" value="发送" />
</div>
</body>
</html>

运行程序发现结果与以上用$.ajax()函数的一致。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值