KingPuff
03-02-2010, 08:20 PM
Hi I'm new to using AJAX and don't really have a complete understanding of it, which is why I believe my code below doesn't work.
basically all this code does is create a request object, loads it with a parameter (a book isbn), and fires it to the server. The response I get back should be a block of XML (verified), but instead I get a null response. Am I building my request object wrong?
note: the alert(responseXml) is just for now me trying to see the block of xml I'm expecting.
const baseUrl = "http://isbndb.com/api/books.xml?access_key=SLMQZCUT";
var ajaxRequest;
function makeHttpObject() {
try {return new XMLHttpRequest();}
catch (error) {}
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch (error) {}
try {return new ActiveXObject("Microsoft.XMLHTTP");}
catch (error) {}
throw new Error("Could not create HTTP request object.");
}
function getBookResultsByIsbnForSeller(isbn) {
ajaxRequest = makeHttpObject();
ajaxRequest.open("GET", baseUrl + "&index1=isbn&value1=" + isbn);
alert(baseUrl + "&index1=isbn&value1=" + isbn);
ajaxRequest.onreadystatechange = getIsbnSearchResponse;
ajaxRequest.send(null);
}
function getIsbnSearchResponse() {
if (ajaxRequest.readyState != 4) return;
if (ajaxRequest.statusText == "OK") {
var responseXml = ajaxRequest.responseXML;
alert(responseXml);
} else alert("Unable to retrieve book information: (" + ajaxRequest.status + ") " + ajaxRequest.statusText);
}
I have tried calling this group of methods using the html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript" src="bookSearch.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script>
getBookResultsByIsbnForSeller("0061031321");
</script>
</body>
</html>
The results I get are:
1) in firefox I get an alert message saying "null"
2) in IE I get nothing
3) in Opera I get nothing
If anyone has any input as to how to get this thing working then please help me out. Thanks a lot!
basically all this code does is create a request object, loads it with a parameter (a book isbn), and fires it to the server. The response I get back should be a block of XML (verified), but instead I get a null response. Am I building my request object wrong?
note: the alert(responseXml) is just for now me trying to see the block of xml I'm expecting.
const baseUrl = "http://isbndb.com/api/books.xml?access_key=SLMQZCUT";
var ajaxRequest;
function makeHttpObject() {
try {return new XMLHttpRequest();}
catch (error) {}
try {return new ActiveXObject("Msxml2.XMLHTTP");}
catch (error) {}
try {return new ActiveXObject("Microsoft.XMLHTTP");}
catch (error) {}
throw new Error("Could not create HTTP request object.");
}
function getBookResultsByIsbnForSeller(isbn) {
ajaxRequest = makeHttpObject();
ajaxRequest.open("GET", baseUrl + "&index1=isbn&value1=" + isbn);
alert(baseUrl + "&index1=isbn&value1=" + isbn);
ajaxRequest.onreadystatechange = getIsbnSearchResponse;
ajaxRequest.send(null);
}
function getIsbnSearchResponse() {
if (ajaxRequest.readyState != 4) return;
if (ajaxRequest.statusText == "OK") {
var responseXml = ajaxRequest.responseXML;
alert(responseXml);
} else alert("Unable to retrieve book information: (" + ajaxRequest.status + ") " + ajaxRequest.statusText);
}
I have tried calling this group of methods using the html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript" src="bookSearch.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<script>
getBookResultsByIsbnForSeller("0061031321");
</script>
</body>
</html>
The results I get are:
1) in firefox I get an alert message saying "null"
2) in IE I get nothing
3) in Opera I get nothing
If anyone has any input as to how to get this thing working then please help me out. Thanks a lot!