Home > Web Front-end > JS Tutorial > Detailed explanation of the steps of js requesting servlet to implement ajax request

Detailed explanation of the steps of js requesting servlet to implement ajax request

php中世界最好的语言
Release: 2018-03-31 15:31:36
Original
2308 people have browsed it

This time I will bring you a detailed explanation of the steps for js to request servlet to implement ajax request. What are the precautions for js to request servlet to implement ajax request? The following is a practical case, let's take a look.

ajax request is a refresh-free user experience that can send two asynchronous requests, GET and POST. The current record is as follows:

GET request:

function sendRequestByGet(){
     //定义异步请求对象
    var xmlReq;
    //检测浏览器是否直接支持ajax
    if(window.XMLHttpRequest){//直接支持ajax
      xmlReq=new XMLHttpRequest();
    }else{//不直接支持ajax
      xmlReq=new ActiveObject('Microsoft.XMLHTTP');
    }
    
     //设置回调函数
     xmlReq.onreadystatechange=function(){
       if (xmlReq.readyState==4&&xmlReq.status==200) {
         //获取服务器的响应值
        var result=xmlReq.responseText;
         //后续操作
         alert(result);
      }
     };
     
     //创建异步get请求
     var url="Hello?name=zhangsan";
     xmlReq.open("GET",url,true);
     //发送请求
     xmlReq.send(null);
   }
Copy after login

POST request :

function sendRequestByPost(){
     //定义异步请求对象
    var xmlReq;
    //检测浏览器是否直接支持ajax
    if(window.XMLHttpRequest){//直接支持ajax
      xmlReq=new XMLHttpRequest();
    }else{//不直接支持ajax
      xmlReq=new ActiveObject('Microsoft.XMLHTTP');
    }
    
     //设置回调函数
     xmlReq.onreadystatechange=function(){
       if (xmlReq.readyState==4&&xmlReq.status==200) {
         //获取服务器的响应值
        var result=xmlReq.responseText;
         //后续操作
         alert(result);
      }
     };
     
     //创建异步Post请求
     var url="Hello";
     xmlReq.open("POST",url,true);
     xmlReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
     //发送请求
     var data="name=lisi";
     xmlReq.send(data);
   }
Copy after login

ajax request servlet:

@Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    String name=req.getParameter("name");
    PrintWriter out = resp.getWriter();
    out.print(name);
  }
Copy after login
Effect:

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related matters on the php Chinese website article!

Recommended reading:

How to pass special character data in Ajax

How to pass an array to the background in Ajax

The above is the detailed content of Detailed explanation of the steps of js requesting servlet to implement ajax request. 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