jquery请求servlet实现ajax异步请求

ajax可以发送异步请求实现无刷新效果,但是使用javascript比较麻烦,就query提供了一些封装的方法 ,可以使得操作更为简单:

$.ajax()方法:

function sendRequest() {
        $.ajax({
            url: "Hello",
            type: "GET",
            dataType: "txt",
            data: "name=zhangsan",
            complete: function(result){
                alert(result.responseText);
            }
        });
    }


$.get()方法:

function sendRequestByGet(){
        $.get("Hello","name=lisi",function(result){
            alert(result);
        });
    }

$.post()方法:

function sendRequestByPost(){
        $.post("Hello","name=wangwu",function(result){
            alert(result);
        });
    }

$.load()方法:

    //load代码等效使用如下$get()方法
    /*
    $get(url, data,  function(result){
                //将result数据填充到页面元素中
        $(“#h2”).html(result);   
    });
    */

    function load(){
        $("h2").load("Hello","name=hahaha");
    }

 

以上异步请求的Hello文件:

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name=req.getParameter("name");
        resp.getWriter().print(name);
    }

 

posted @ 2016-04-13 20:01  我爱Java  阅读(12563)  评论(0编辑  收藏  举报