Home > Web Front-end > JS Tutorial > body text

jquery request servlet to implement ajax asynchronous request example sharing

小云云
Release: 2018-01-10 10:44:02
Original
1540 people have browsed it

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);
      }
    });
  }
Copy after login

$.get() method:

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

$.post() method:

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

$.load() Method:

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

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

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);
  }
Copy after login

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

Servlet generates html page

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!