0

I am working on an App and I am trying to do xmlhttp request in nodejs and express but my responseXML is not working and it returns nothing.. Here is my code:-

var XMLHttpRequest=require("xmlhttprequest").XMLHttpRequest;
var xml2js=require("xml2js");


var xhr=new XMLHttpRequest();

xhr.onreadystatechange=function(){

    console.log("State:"+this.readyState);

    if(this.readyState==4){


        var result=this.responseXML;
        console.log(result);


    }

};

xhr.open("GET","http://isbndb.com/api/books.xml?access_key=QMMEUNJB&results=prices&index1=isbn&value1=0061031321");
xhr.setRequestHeader('Content-type', 'text/xml');

xhr.send();

And the output is:-

Output:-
State:1
State:1
State:2
State:3
State:4

It returns a blank line after State:4

4

1 回答 1

1

The xmlhttprequest package doesn't (currently) support XML - see the bottom of https://npmjs.org/package/xmlhttprequest

You're already including the xml2js library, but your code doesn't appear to be using it anywhere. Try replacing

var result=this.responseXML;
console.log(result);

with this:

xml2js.parseString(this.responseText, function (err, result) {
    console.dir(result);
});
于 2013-03-29T21:05:34.887 回答