zoukankan      html  css  js  c++  java
  • 怎样发出一个HTTP请求

    需要使用 xhr.send(); 参数为请求数据体, 如果没有就传入null, 一般来说, GET请求是不用传参的, POST就视情况而定, 理论上所有GET请求都可以改为POST, 反之则不行.

    var xhr = new XMLHttpRequest();
    xhr.open('GET',
      'http://www.example.com/?id=' + encodeURIComponent(id),
      true
    );
    xhr.send(null);

    下面是POST请求的示例: 

    var xhr = new XMLHttpRequest();
    var data = 'email='
      + encodeURIComponent(email)
      + '&password='
      + encodeURIComponent(password);
    
    xhr.open('POST', 'http://www.example.com', true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send(data);

    注意: 

    1. 所有xhr的监听事件都必须在xhr.send()方法调用之前声明;

    2. xhr.send()的参数类型支持多种, 比如: null / ArrayBufferView / Blob / Document / String / FormData;

  • 相关阅读:
    wc 统计程序
    读C#程序
    VS2013 单元测试
    android开发心得之知识的量变到质变
    大学第一篇博客
    团队作业七
    团队作业六
    团队作业五
    团队作业四
    团队作业三
  • 原文地址:https://www.cnblogs.com/aisowe/p/11558153.html
Copyright © 2011-2022 走看看