Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

responseXML not being set? #8

Closed
ghost opened this issue Jun 22, 2011 · 3 comments
Closed

responseXML not being set? #8

ghost opened this issue Jun 22, 2011 · 3 comments

Comments

@ghost
Copy link

ghost commented Jun 22, 2011

Hello,

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);
}
}
}

@dfellis
Copy link
Contributor

dfellis commented 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
Copy link
Owner

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.

@ghost
Copy link

ghost commented Jan 11, 2012

Hello,

I implemented responseXML in my fork of node-XMLHttpRequest
using "jsdom" (using "htmlparser"). The benefit is that you can parse
non XML url like any HTML website and get a DOM.

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.

I implemented it within an exception handler so node-XMLHttpRequest
should continue to work even if jsdom has not been installed. Of
course without those modules responseXML cannot be used but
responseText will continue to work.

I hope Dan (driverdan) will add my patch into the mainstream.

The implementation :
https://github.com/lordbaco/node-XMLHttpRequest/commit/1303a2596ad7fad504a1e1d4c696ee76b78c6d9a

To use this feature you need to :
npm install jsdom
git clone https://lordbaco@github.com/lordbaco/node-XMLHttpRequest.git
then use it in your code

var sys = require('util');
var XMLHttpRequest = require("./node-XMLHttpRequest/XMLHttpRequest").XMLHttpRequest;
var url = 'https://github.com';
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4) {
var document = this.responseXML;
//sys.puts("XML:\n" + sys.inspect(document.innerHTML, false, null));
var nodes = document.body.getElementsByTagName('*');
for (var i=0; i<nodes.length; i++) {
// ...
sys.print(nodes[i].nodeName.toLowerCase() + "\n");
}
}
};
xhr.open("GET", url);
xhr.send();

Best Regards,
Guy (Lordbaco)

mrcarlberg pushed a commit to mrcarlberg/node-XMLHttpRequest that referenced this issue May 27, 2021
…thname_when_loading_from_local_filesystem

Unescape pathname from url when loading from local filesystem
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants