Click to See Complete Forum and Search --> : XML with AJAX


senthilmca
03-13-2006, 01:44 AM
Hai!

How to read datas from xml using AJAX based on Mozilla and Netscape browser comatibility. Pl give sample code. My code is working fine in Internet Explorer but it wont run in Mozilla.

Thanks

bathurst_guy
03-13-2006, 01:50 AM
Can you post what you have so that we can just include the parts that are missing in order to make it compatible?

senthilmca
03-13-2006, 03:05 AM
hai this is my code: This code works fine in IE message will be retrived from XML file and shown in alert. But in Mozilla undefined is displayed in alert message

enterid.html

<html>
<head>
<title>New Page 1</title>

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">

var req;
var target;
var isIE;
var obj;
var keyTxt;
var xmlDoc;
var tipsTxt;
var ijk;


function initRequest() {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}

function validateuserid() {

target=document.getElementById("UserID").value;
var url = "selectid_file.jsp?id=" + escape(target);
initRequest();
req.onreadystatechange = processRequest;
req.open("GET", url, true);
req.send(null);
}


function processRequest() {

if (req.readyState == 4) {
if (req.status == 200) {
// code for IE
if (window.ActiveXObject)
{
alert("IE");
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("tips.xml");

readXML();
}
// code for Mozilla, etc.
else if (document.implementation && document.implementation.createDocument)
{
alert("Mozilla");
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.async='false';
xmlDoc.load('tips.xml');
xmlDoc.onload=readXML;
}
else{

alert('Your browser cannot handle this script');
}
}
}
}


function readXML(){

obj=xmlDoc.lastChild;
if(obj.childNodes.length > 0)
{
var ij;
for (ij = 0;ij < obj.childNodes.length;ij++)
{
for (j=0;j<obj.childNodes[ij].attributes.length;j++)
{
if(obj.childNodes[ij].attributes[j].nodeName == 'KEY' )
{
tipsTxt = obj.childNodes[ij].text;

var arr=new Array(tipsTxt);

var msg = "";
for (x = 0; x < arr.length; x++)
{

alert(arr[x]);

}
}
}

}

}
}

</SCRIPT>

</head>

<body>
<form name="f1" method="GET" action="">
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
ID VALIDATION USING AJAX</p>
<div align="center">
<center>
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="39%" id="AutoNumber1">
<tr>
<td width="50%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<b>Login ID</b></td>
<td width="50%">

<input type="text" id="UserID" name="UserID" size="20">

</td>
</tr>
<tr>
<td width="50%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Use Name&nbsp;&nbsp;&nbsp;&nbsp; </td>

<td width="50%">
<input type="text" name="userName" onClick="validateuserid()" size="20"></td>
</tr>
<tr>
<td width="50%">&nbsp;</td>

<td width="50%">
</td>
</tr>
<tr>
<td width="50%">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" value="Submit" name="B1"></td>
<td width="50%"><input type="reset" value="Reset" name="B2"></td>
</tr>
</table>
</center>
<div id="userIdMessage"></div>
</div>
</form>
</span>
</body>

</html>


selectid_file.jsp


<%@ page language="java" import="java.sql.*,java.util.*,java.io.*" %>

<%

String loginID=request.getParameter("id");
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
String DATE_FORMAT = "MMM-yyyy-dd HH:mm:ss";

java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(DATE_FORMAT);

sdf.setTimeZone(TimeZone.getDefault());

String toDayTime=sdf.format(cal.getTime());

String msg ="Hai";

try{

// File outputFile = new File("C:/jakarta-tomcat-5.5.4/webapps/ROOT/npajax/tps.xml");
File outputFile = new File("C:/jakarta-tomcat-5.5.4/webapps/ROOT/npajax/tips.xml");
// File outputFile = new File("tips1.xml");

outputFile.createNewFile();

FileWriter outFile = new FileWriter(outputFile);




outFile.write("<Tips>");



outFile.write("<Content KEY='TIME'>");
outFile.write("<text>");
outFile.write(toDayTime);
outFile.write("</text>");
outFile.write("</Content>");

outFile.write("<Content KEY='LID'>");
outFile.write("<text>");
outFile.write(loginID);
outFile.write("</text>");
outFile.write("</Content>");

outFile.write("<Content KEY='MSSG'>");
outFile.write("<text>");
outFile.write(msg);
outFile.write("</text>");
outFile.write("</Content>");

outFile.write("</Tips>");
outFile.close();

}catch(IOException e1)
{
System.out.println(e1);
}

%>

DrBarney
04-06-2006, 08:41 AM
Clearly your active x object is only specified for a Microsoft client. This works nicely for Mozilla.



function initialiseXmlHttp ()
{
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e)
{
xmlhttp=false
}
}

if (!xmlhttp)
{
try
{
xmlhttp = new XMLHttpRequest();
}
catch (e)
{
xmlhttp=false;
}
}
return xmlhttp;
}




Handling can then be consistent for all browser types.

Good Luck

phpnovice
04-16-2006, 10:54 PM
Clearly your active x object is only specified for a Microsoft client.
Why do you say that? The OP has the following in the posted code:

function initRequest() {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
}
}

Webnerd
04-18-2006, 07:56 AM
This has worked flawlessly for me across all DOM platforms

var http_request = false;
var _ms_XMLHttpRequest_ActiveX="";

function makeRequest(url) {

http_request = false;

if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType("text/xml");
}
} else if (window.ActiveXObject) { // IE

var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];

for (var i = 0; i < versions.length ; i++) {
try {
http_request = new ActiveXObject(versions[i]);

if (http_request) {
_ms_XMLHttpRequest_ActiveX = versions[i];
break;
}
}
catch (objException) {
} ;
}
}

if (!http_request) {
alert("Cannot create an XMLHTTP instance");
return false;
}
http_request.onreadystatechange = parseResults;
http_request.open('GET', url, true);
http_request.send(null);

}

function parseResults() {

if (http_request.readyState == 4) {

if (http_request.status == 200) {

//var xmlDom=http_request.responseXML;
//or

var xmlDom=http_request.responseText;


alert(responseText);


} else {

alert("There was a problem with the request.");

return;
}
}
}