且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我不能在亚马逊服务器上使用新的XMLHttpRequest?

更新时间:2022-06-12 23:32:15

除非您使用的是类似 https的东西://github.com/driverdan/node-XMLHttpRequest 节点中没有XMLHttpRequest. XMLHttpRequest是仅在浏览器中存在的对象.

Unless you are using something like https://github.com/driverdan/node-XMLHttpRequest there's no XMLHttpRequest in node. XMLHttpRequest is an object that only exists in browsers.

您必须要求http才能在节点中发出http请求.

You have to require http to make http requests in node.

var http = require('http');

然后您可以执行类似的操作(从文档中获取):>

Then you can do something like that (taken from the docs):

var postData = querystring.stringify({
  'msg' : 'Hello World!'
});

var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': postData.length
  }
};

var req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.')
  })
});

req.on('error', (e) => {
  console.log(`problem with request: ${e.message}`);
});

// write data to request body
req.write(postData);
req.end(

);