Home > Web Front-end > JS Tutorial > Introduction to Ajax get and post requests

Introduction to Ajax get and post requests

不言
Release: 2018-07-02 16:29:23
Original
1614 people have browsed it

The following article brings you an in-depth understanding of Ajax get and post requests. The content is quite good, so I will share it with you now and give it as a reference.

1.get request

function ()
 {
    //向服务器请求 时间
    //1.创建异步对象(小浏览器)
    var xhr = new XMLHttpRequest();
    //2.设置参数,true表示使用异步模式
    xhr.open("get", "GetTime.ashx?name= Mr靖", true); 
    //3.让get请求不从浏览器获取缓存数据
    xhr.setRequestHeader("If-Modified-Since","0");
    //3.设置回调函数
    xhr.onreadystatechange = function ()
    {
          //3.1当完全接收完响应报文后 并且 响应状态码为200的时候
          if (xhr.readyState == 4 && xhr.status == 200)
          {
            //3.2获取相应报文体内容
            var res = xhr.responseText;
            alert(res);
          }
    };
    //4.发送异步请求
    xhr.send(null);
}
Copy after login

2.post request

function ()
 {
    //向服务器请求 时间
    //1.创建异步对象(小浏览器)
    var xhr = new XMLHttpRequest();
    //2.设置参数
    xhr.open("post", "GetTime.ashx", true);
    //3.设置 请求 报文体 的 编码格式(设置为 表单默认编码格式)
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //4.设置回调函数
    xhr.onreadystatechange = function ()
    {
        //3.1当完全接收完响应报文后 并且 响应状态码为200的时候
        if (xhr.readyState == 4 && xhr.status == 200)
       {
         //3.2获取相应报文体内容
          var res = xhr.responseText;
          alert(res);
       }
     };
     //5.发送异步请求"name=Mr靖"
     //5.1格式:直接拼接字符串 key=value&key1=value2
     xhr.send("name=Mr靖&age=18");
 };
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone Learning will be helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

AJAX uses post to send data in xml format to receive data

Re-encapsulate and re-encapsulate ajax in Jquery Simplified operation

The above is the detailed content of Introduction to Ajax get and post requests. 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