this.responseXML is not working while making request to isbndb server

this.responseXML is not working while making request to isbndb server

Problem

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

Problem courtesy of: Tarun

Solution

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);
});
Solution courtesy of: Nathan Friedly

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *