XMLHttpRequest POST数据未发送

时间:2018-12-14 13:25:57

标签: javascript xmlhttprequest

我正在尝试使用XMLHttpRequest将一些数据发布到我的服务器上。请求发生了,但是没有发送数据:

  getLinkPreview = function(url) {
    return new Promise(function(resolve, reject) {
      var xmlhttp = new XMLHttpRequest();
      var postData = 'url=' + url
      xmlhttp.open('POST', '/link_preview', true);
      xmlhttp.setRequestHeader('Content-length', postData.length);
      xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

      xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == XMLHttpRequest.DONE) {
          if (xmlhttp.status == 200) {
            var response = xmlhttp.responseText;
            var preview = null;
            try {
              preview = JSON.parse(response);
            } catch (err) {
              reject(err);
              return;
            }
            resolve(preview);
          } else {
            reject(new Error('status_' + xmlhttp.status));
          }
        }
      };

      xmlhttp.send(postData); 
    });
  }

然后我这样称呼它:

getLinkPreview(url).then(function(response) {
  console.log(response);
});

我已经在Internet上拖网了,它看起来好像在做正确的事,但似乎肯定缺少我所缺少的东西。

以下是设置的请求标头:

Host: localhost:8000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: */*
Accept-Language: en-GB,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:8000/chat.html
Content-Type: application/x-www-form-urlencoded
Content-Length: 63
Connection: keep-alive
Cookie: __utma=111872281.409324153.1529435407.1540323630.1540367598.5; __utmz=111872281.1529435407.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); _patchwork_login=BAhJIh1hZ2VudDRAcGF0Y2h3b3JrdGVzdC5jb20GOgZFVA%3D%3D--669f735e18474e5e194a212660bb42ccdb1d6d2f; _ga=GA1.1.409324153.1529435407; botkit_guid=2e866d66-17fa-45b0-efa3-84fdd05a473a
DNT: 1

0 个答案:

没有答案