You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to use node-XMLHttpRequest to make AJAX calls from JavaScript code running in node.js. The JavaScript code calls a simple PHP-based server app, which returns some XML (see below). The code works fine when running in a browser, but it does not work properly when runing in node.js using node-XMLHttpRequest. The call to the server completes, but there does not seem to be any data in responseXML. Instead, node.js gives me the error:
var timeValue = http.responseXML.getElementsByTagName("timenow")[0
^
TypeError: Object has no method 'getElementsByTagName'
The complete text returned by the server is contained in http.responseText. When/how can that text be parsed into XML so that I can grab the "timenow" field? Looking at the code for node-XMLHttpRequest, it looks like only the responseText variable is filled (on line 212) when a "data" response is received. I see almost no references to the responseXML variable.
Is there some other code required for filling/scanning the responseXML variable, which still has to be implemented in node-XMLHttpRequest? Or am I not calling the function properly to receive data in XML format?
The code is below. Thanks for any tips or pointers.
--IO
CLIENT CODE (JS):
var http = new XMLHttpRequest();
function getServerTime() {
var myurl = 'http://kundry/qrate.php';
myRand = parseInt(Math.random() * 999999999999999);
var modurl = myurl+"?rand="+myRand;
http.open("get", modurl, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}
function useHttpResponse() {
if (http.readyState == 4) {
if (http.status == 200) {
var timeValue = http.responseXML.getElementsByTagName("timenow")[0];
console.log("timenow="+timeValue.childNodes[0].nodeValue);
}
}
}
Hey, while there's really only one XMLHttpRequest project for Node.js, there are many XML parsers with different plusses and minuses, with twodifferent XML-DOM-compliant implementations out there.
Well, the second is almost-compliant, but intentionally drops a few infrequently-used features for faster speed.
Note: I'm in no way related to either of those projects, and the data interchange format of choice at the startup I work at is JSON-RPC, so I haven't used either, just going off of their respective descriptions.
This library is pretty much a standalone, only-core-library-dependencies project, which is probably why its that way right now.
If you want the interface to be identical (so the code can run both client and server), forking the project is probably your best bet.
node-XMLHttpRequest doesn't currently support parsing XML. As David mentioned this is because there is no native XML parsing in node and I haven't really thought about requiring external libraries.
Additionally you're relying on getElementsByTagName which will require an XML library that supports DOM functions.
Activity
dfellis commentedon Jun 23, 2011
Hey, while there's really only one XMLHttpRequest project for Node.js, there are many XML parsers with different plusses and minuses, with two different XML-DOM-compliant implementations out there.
Well, the second is almost-compliant, but intentionally drops a few infrequently-used features for faster speed.
Note: I'm in no way related to either of those projects, and the data interchange format of choice at the startup I work at is JSON-RPC, so I haven't used either, just going off of their respective descriptions.
This library is pretty much a standalone, only-core-library-dependencies project, which is probably why its that way right now.
If you want the interface to be identical (so the code can run both client and server), forking the project is probably your best bet.
driverdan commentedon Jun 23, 2011
node-XMLHttpRequest doesn't currently support parsing XML. As David mentioned this is because there is no native XML parsing in node and I haven't really thought about requiring external libraries.
Additionally you're relying on getElementsByTagName which will require an XML library that supports DOM functions.
Merge pull request driverdan#8 from mrcarlberg/mjwwit_unescape_url_pa…