Home > Article > Web Front-end > jquery request servlet to implement ajax asynchronous request example sharing
This article mainly brings you an example of jquery requesting servlet to implement ajax asynchronous request. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.
ajax can send asynchronous requests to achieve no refresh effect, but using javascript is more troublesome. Query provides some encapsulation methods to make the operation simpler:
$.ajax() method:
function sendRequest() { $.ajax({ url: "Hello", type: "GET", dataType: "txt", data: "name=zhangsan", complete: function(result){ alert(result.responseText); } }); }
$.get() method:
function sendRequestByGet(){ $.get("Hello","name=lisi",function(result){ alert(result); }); }
$.post() method:
function sendRequestByPost(){ $.post("Hello","name=wangwu",function(result){ alert(result); }); }
$.load() Method:
//load代码等效使用如下$get()方法 /* $get(url, data, function(result){ //将result数据填充到页面元素中 $(“#h2”).html(result); }); */ function load(){ $("h2").load("Hello","name=hahaha"); }
Hello file for the above asynchronous request:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name=req.getParameter("name"); resp.getWriter().print(name); }
Related recommendations:
Servlet+Ajax to implement the smart search box smart prompt function
Implementation steps of js and servlet to implement file upload in h5
The above is the detailed content of jquery request servlet to implement ajax asynchronous request example sharing. For more information, please follow other related articles on the PHP Chinese website!