jquery請求servlet實現ajax異步請求

歡迎訪問我的博客:www.yyxxk.comjavascript

ajax能夠發送異步請求實現無刷新效果,可是使用javascript比較麻煩,就query提供了一些封裝的方法 ,可使得操做更爲簡單:html

$.ajax()方法:java

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


$.get()方法:ajax

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

$.post()方法:異步

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

$.load()方法:post

    //load代碼等效使用以下$get()方法
    /*
    $get(url, data,  function(result){
                //將result數據填充到頁面元素中
        $(「#h2」).html(result);   
    });
    */

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

 

以上異步請求的Hello文件:url

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String name=req.getParameter("name");
        resp.getWriter().print(name);
    }
相關文章
相關標籤/搜索