Home  >  Article  >  Web Front-end  >  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中世界最好的语言
php中世界最好的语言Original
2018-03-31 15:31:362170browse

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);
   }

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);
   }

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);
  }
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!

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
Previous article:How to use Django AjaxNext article:How to use Django Ajax