美文网首页
JS访问URL

JS访问URL

作者: 毛不翼 | 来源:发表于2020-08-07 16:50 被阅读0次

    var httpRequest = new XMLHttpRequest();//第一步:创建需要的对象
    var url = "http://dev-ge.back.jd.com/eco/index?host-ip=11.19.112.122"
    httpRequest.withCredentials=true;
    httpRequest.open('POST', url, true); //第二步:打开连接
    httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");//设置请求头 注:post方式必须设置请求头(在建立连接后设置请求头)
    httpRequest.send('name=teswe&ee=ef');//发送请求 将情头体写在send中
    /**

    • 获取数据后的处理程序
      */
      httpRequest.onreadystatechange = function () {//请求后的回调接口,可将请求成功后要执行的程序写在其中
      if (httpRequest.readyState == 4 && httpRequest.status == 200) {//验证请求是否发送成功
      var json = httpRequest.responseText;//获取到服务端返回的数据
      console.log(json);
      }
      };

    相关文章

      网友评论

          本文标题:JS访问URL

          本文链接:https://www.haomeiwen.com/subject/jqtqdktx.html