zoukankan      html  css  js  c++  java
  • AJAX---遵循http协议

    遵循http协议

    本质上 XMLHttpRequest 就是 JavaScript 在 Web 平台中发送 HTTP 请求的手段,

    所以我们发送出去的请求任然是 HTTP 请求,同样符合 HTTP 约定的格式

        var xhr = new XMLHttpRequest()
        //设置请求报文的请求行
        xhr.open('GET', '/time.php');
        //设置请求头
        xhr.setRequestHeader('Accept', 'text/plain');
        //设置请求体
        xhr.send(null);
        xhr.onreadystatechange = function () {
          if (this.readyState === 4) {
            // 获取响应状态码 200
            console.log(this.status)
            // 获取响应状态描述 ok
            console.log(this.statusText)
            // 获取响应头信息
            console.log(this.getResponseHeader('Content‐Type')) // 指定响应头
            console.log(this.getAllResponseHeader()) // 全部响应头
            // 获取响应体
            console.log(this.responseText) // 文本形式
            console.log(this.responseXML) // XML 形式,了解即可不用了
          }
        }

    1. 一旦你的请求体是 urlencoded 格式的内容,一定要设置请求头中 Content-Type 'application/x-www-form-urlencoded'
    2. 也就是,请求头的content-Type的格式,要和请求体一致

        xhr.open('POST', '/add.php') // 设置请求行
    
        xhr.setRequestHeader('Foo', 'Bar') // 设置一个请求头
        // 一旦你的请求体是 urlencoded 格式的内容,一定要设置请求头中 Content-Type 'application/x-www-form-urlencoded'
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
        //发送的是什么格式,上面请求头的content-Type应该匹配
        xhr.send('key1=value1&key2=value2') // 以 urlencoded 格式设置请求体
  • 相关阅读:
    scrapy框架
    selenium解析
    xpath解析
    解析语法
    request-html-render
    牛逼的requests-html
    Beautifulsoup
    请求和响应
    reuqests请求
    Django文件上传下载与富文本编辑框
  • 原文地址:https://www.cnblogs.com/jane-panyiyun/p/12269997.html
Copyright © 2011-2022 走看看