Home  >  Article  >  Web Front-end  >  jquery request servlet to implement ajax asynchronous request example sharing

jquery request servlet to implement ajax asynchronous request example sharing

小云云
小云云Original
2018-01-10 10:44:021541browse

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

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!

Statement:
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