如何在Node.js中创建xhr请求?这是我的js文件:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var url="https://xx.xxx.xxx.xxx/server";
function soap() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', url, true);
xmlhttp.setRequestHeader("SOAPAction", "https://xx.xxx.xxx.xxx/server/myLoginProcess");
// build SOAP request
var sr =
'<?xml version="1.0" encoding="UTF-8"?>'+
'<SOAP-ENV:Envelope xmlns:SOAP-ENV=http://schemas.xmlsoap.org/soap/envelope/'+
'xmlns:ns1=https://xx.xxx.xxx.xxx/server/'+
'xmlns:xsd=http://www.w3.org/2001/XMLSchema/'+
'xmlns:SOAP-ENC=http://schemas.xmlsoap.org/soap/encoding/'+
'SOAP-ENV:encodingStyle=http://schemas.xmlsoap-org/encoding/>'+
'<SOAP-ENV:Body>'+
'<ns1:ihLoginProcess>'+
'<sEmail xsi:type="xsd:string">me@mail.de</sEmail>'+
'<sPasswort xsi:type="xsd:string">password</sPasswort>'+
'<sDeviceID xsi:type="xsd:string">00-14-22-01-23-45</sDeviceID>'+
'</ns1:ihLoginProcess>'+
'</SOAP-ENV:Body>'+
'</SOAP-ENV:Envelope>';
xmlhttp.onreadystatechange = function ()
{
console.log("onreadystatechange ");
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
console.log("status 200");
console.log('done !');
}
}else if (xmlhttp.readyState == 1){
console.log("readyState 1");
}
}
// Send the POST request
xmlhttp.setRequestHeader('Access-Control-Allow-Origin', '*');
xmlhttp.setRequestHeader('Content-Type', 'text/xml;; charset="utf-8"');
xmlhttp.send(sr);
// send request
// ...
}
soap();
在我写的终端中: 节点myJSFile.js
结果是:
onreadystatechange
readyState 1
onreadystatechange
但它应该是readyState 4并且应该将sr发送到服务器。因此,我应该返回一个XML字符串。