•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 401,622 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,794 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 4573 | Replies: 24 | Solved
![]() |
Hi friends,
i have a problem in ajax.The code will get values when i use IE but it doesnt work with mozilla browser.It showing the alert message ? I am adding the code along with this.Expecting the reply soon.
var xmlHttp;
function getXmlHttp()
{
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try {
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
alert("Please use a new browser!");
return false;
}
}
}
}
function user()
{
document.getElementById('form').style.visibility = "visible";
var id=document.getElementById('id').value;
getXmlHttp();
var myurl = "<%=request.getContextPath()%>/AjaxUser.do?type=ajax&id="+id;
xmlHttp.open("GET", myurl , true);
xmlHttp.onreadystatechange = useHttpResponseSuggestId;
xmlHttp.send(null);
}
function useHttpResponseSuggestId()
{
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var textout = xmlHttp.responseText;
try
{
var x= new DOMParser().parseFromString(textout, 'text/xml');
xmldom.async="false";
xmldom.loadXML(x);
alert(xmldom);
xmlObj=xmldom.documentElement;
var master = [document.getElementById('name'),document.getElementById('password'),
document.getElementById('realname'),document.getElementById('email'),
document.getElementById('title'),document.getElementById('desc')];
var length=xmlObj.childNodes.length;
for(var i=0;i<master.length;i++)
{
master[i].value = xmlObj.childNodes[i].firstChild.nodeValue;
}
}
catch(e)
{
try
{
var xmldom = new ActiveXObject('Microsoft.XMLDOM');
xmldom.async="false";
xmldom.loadXML(textout);
xmlObj=xmldom.documentElement;
var master = [document.getElementById('name'),document.getElementById('password'),
document.getElementById('realname'),document.getElementById('email'),
document.getElementById('title'),document.getElementById('desc')];
var length=xmlObj.childNodes.length;
for(var i=0;i<master.length;i++)
{
master[i].value = xmlObj.childNodes[i].firstChild.nodeValue;
}
}
catch(e)
{
alert("Please use a new browser! This browser does not support XML!");
}
}
}
}
}
i am getting this alert("Please use a new browser!");
while using mozilla....
i have a problem in ajax.The code will get values when i use IE but it doesnt work with mozilla browser.It showing the alert message ? I am adding the code along with this.Expecting the reply soon.
var xmlHttp;
function getXmlHttp()
{
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try {
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
alert("Please use a new browser!");
return false;
}
}
}
}
function user()
{
document.getElementById('form').style.visibility = "visible";
var id=document.getElementById('id').value;
getXmlHttp();
var myurl = "<%=request.getContextPath()%>/AjaxUser.do?type=ajax&id="+id;
xmlHttp.open("GET", myurl , true);
xmlHttp.onreadystatechange = useHttpResponseSuggestId;
xmlHttp.send(null);
}
function useHttpResponseSuggestId()
{
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var textout = xmlHttp.responseText;
try
{
var x= new DOMParser().parseFromString(textout, 'text/xml');
xmldom.async="false";
xmldom.loadXML(x);
alert(xmldom);
xmlObj=xmldom.documentElement;
var master = [document.getElementById('name'),document.getElementById('password'),
document.getElementById('realname'),document.getElementById('email'),
document.getElementById('title'),document.getElementById('desc')];
var length=xmlObj.childNodes.length;
for(var i=0;i<master.length;i++)
{
master[i].value = xmlObj.childNodes[i].firstChild.nodeValue;
}
}
catch(e)
{
try
{
var xmldom = new ActiveXObject('Microsoft.XMLDOM');
xmldom.async="false";
xmldom.loadXML(textout);
xmlObj=xmldom.documentElement;
var master = [document.getElementById('name'),document.getElementById('password'),
document.getElementById('realname'),document.getElementById('email'),
document.getElementById('title'),document.getElementById('desc')];
var length=xmlObj.childNodes.length;
for(var i=0;i<master.length;i++)
{
master[i].value = xmlObj.childNodes[i].firstChild.nodeValue;
}
}
catch(e)
{
alert("Please use a new browser! This browser does not support XML!");
}
}
}
}
}
i am getting this alert("Please use a new browser!");
while using mozilla....
Can't see why your codes doesn't invoke xmlHttp=new XMLHttpRequest(); bit odd.
Just a suggestion:
What you want to do when creating an XMLHttpRequest() Object is to test for the W3C implementation before IE6 and older implementation. The reason is that IE7 still supports the IE6 XHR object which is created via ActiveX.
Simple example to replace the code above. See if it works:
ref: http://www.w3schools.com/xml/xml_http.asp
var xmlHttp;
function getXmlHttp()
{
try {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try {
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
alert("Please use a new browser!");
return false;
}
}
}
}Just a suggestion:
What you want to do when creating an XMLHttpRequest() Object is to test for the W3C implementation before IE6 and older implementation. The reason is that IE7 still supports the IE6 XHR object which is created via ActiveX.
Simple example to replace the code above. See if it works:
xmlHttp=null;
function getXmlHttp() {
xmlHttp=null;
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
// code for IE
else if (window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else alert("Please use a new browser!");
return xmlHttp;
}ref: http://www.w3schools.com/xml/xml_http.asp
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Actually the problem may be in this code
if(xmlHttp.status == 200)
{
var textout = xmlHttp.responseText;
try
{
var x= new DOMParser().parseFromString(textout, 'text/xml');
xmldom.async="false";
xmldom.loadXML(x);
alert(xmldom);
xmlObj=xmldom.documentElement;
var master = [document.getElementById('name'),document.getElementById('password'),
document.getElementById('realname'),document.getElementById('email'),
document.getElementById('title'),document.getElementById('desc')];
var length=xmlObj.childNodes.length;
for(var i=0;i<master.length;i++)
{
master[i].value = xmlObj.childNodes[i].firstChild.nodeValue;
}
}
I am getting the textout value as xml in IE but didnt get the values in Mozilla browser
if(xmlHttp.status == 200)
{
var textout = xmlHttp.responseText;
try
{
var x= new DOMParser().parseFromString(textout, 'text/xml');
xmldom.async="false";
xmldom.loadXML(x);
alert(xmldom);
xmlObj=xmldom.documentElement;
var master = [document.getElementById('name'),document.getElementById('password'),
document.getElementById('realname'),document.getElementById('email'),
document.getElementById('title'),document.getElementById('desc')];
var length=xmlObj.childNodes.length;
for(var i=0;i<master.length;i++)
{
master[i].value = xmlObj.childNodes[i].firstChild.nodeValue;
}
}
I am getting the textout value as xml in IE but didnt get the values in Mozilla browser
•
•
•
•
Actually the problem may be in this code
if(xmlHttp.status == 200)
{
var textout = xmlHttp.responseText;
try
{
var x= new DOMParser().parseFromString(textout, 'text/xml');
xmldom.async="false";
xmldom.loadXML(x);
alert(xmldom);
xmlObj=xmldom.documentElement;
var master = [document.getElementById('name'),document.getElementById('password'),
document.getElementById('realname'),document.getElementById('email'),
document.getElementById('title'),document.getElementById('desc')];
var length=xmlObj.childNodes.length;
for(var i=0;i<master.length;i++)
{
master[i].value = xmlObj.childNodes[i].firstChild.nodeValue;
}
}
I am getting the textout value as xml in IE but didnt get the values in Mozilla browser
Good Call.
You should use
var textout = xmlHttp.responseXML;
instead of
var textout = xmlHttp.responseText;
and remove the DOMParser() functions.
You have to either make sure your server sends the correct XML headers when serving the response.
eg in PHP:
header('Content-Type: text/xml');OR you can force the browser to treat the response as XML.
xmlHttp.overrideMimeType('text/xml');Then the XML document will be available for traversal/manipulation using DOM Interface.
Last edited by digital-ether : Jan 17th, 2008 at 6:45 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
You can also replace:
with:
this is the correct usage of DOMParser().
It is however still better for you to get the XMLHttpRequest response as XML instead of the way it is done in your example where you are retrieving it as Text and then using browser specific XML/DOM parsers to convert the text to XML.
var x= new DOMParser().parseFromString(textout, 'text/xml'); xmldom.async="false"; xmldom.loadXML(x); alert(xmldom); xmlObj=xmldom.documentElement;
with:
var xmldom = new DOMParser().parseFromString(textout, 'text/xml'); xmlObj=xmldom.documentElement;
this is the correct usage of DOMParser().
It is however still better for you to get the XMLHttpRequest response as XML instead of the way it is done in your example where you are retrieving it as Text and then using browser specific XML/DOM parsers to convert the text to XML.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
The problem is again in mozilla browser
Below is my index.jsp page in that i written the script of ajax
var xmlHttp;
function getXmlHttp()
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try {
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
alert("Please use a new browser!");
return false;
}
}
}
}
function ajax()
{
var name=document.getElementById('name').value;
getXmlHttp();
var myurl="<%=request.getContextPath()%>/AjaxController?type=ajax&name="+name;
alert(myurl);
xmlHttp.open("GET", myurl , true);
xmlHttp.onreadystatechange = useHttpResponseSuggestId;
xmlHttp.send(null);
}
function useHttpResponseSuggestId()
{
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var textout = xmlHttp.responseText;
alert(textout);
try
{
var xmldom = new DOMParser().parseFromString(textout, 'text/xml');
xmlObj=xmldom.documentElement;
var master = document.getElementById('ajaxname');
master.value=xmlObj.childNodes[0].firstChild.nodeValue;
}
catch(e)
{
try
{
var xmldom = new ActiveXObject('Microsoft.XMLDOM');
xmldom.async="false";
xmldom.loadXML(textout);
xmlObj=xmldom.documentElement;
var master = document.getElementById('ajaxname');
master.value = xmlObj.childNodes[0].firstChild.nodeValue;
}
catch(e)
{
alert("Please use a new browser! This browser does not support XML!");
}
}
}
}
}
Below is my index.jsp page in that i written the script of ajax
var xmlHttp;
function getXmlHttp()
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
try {
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
alert("Please use a new browser!");
return false;
}
}
}
}
function ajax()
{
var name=document.getElementById('name').value;
getXmlHttp();
var myurl="<%=request.getContextPath()%>/AjaxController?type=ajax&name="+name;
alert(myurl);
xmlHttp.open("GET", myurl , true);
xmlHttp.onreadystatechange = useHttpResponseSuggestId;
xmlHttp.send(null);
}
function useHttpResponseSuggestId()
{
if (xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
var textout = xmlHttp.responseText;
alert(textout);
try
{
var xmldom = new DOMParser().parseFromString(textout, 'text/xml');
xmlObj=xmldom.documentElement;
var master = document.getElementById('ajaxname');
master.value=xmlObj.childNodes[0].firstChild.nodeValue;
}
catch(e)
{
try
{
var xmldom = new ActiveXObject('Microsoft.XMLDOM');
xmldom.async="false";
xmldom.loadXML(textout);
xmlObj=xmldom.documentElement;
var master = document.getElementById('ajaxname');
master.value = xmlObj.childNodes[0].firstChild.nodeValue;
}
catch(e)
{
alert("Please use a new browser! This browser does not support XML!");
}
}
}
}
}
its calling the ajaxcontroller.java file which was written below
AjaxController.java
public class AjaxController extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
System.out.println("/Ajax Controller");
PrintWriter out = response.getWriter();
out.println("<?xml version=\"1.0\"?>");
out.println("<code>");
out.println("<name>sree</name>");
out.println("</code>");
out.close();
out.flush();
response.flushBuffer();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
AjaxController.java
public class AjaxController extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
System.out.println("/Ajax Controller");
PrintWriter out = response.getWriter();
out.println("<?xml version=\"1.0\"?>");
out.println("<code>");
out.println("<name>sree</name>");
out.println("</code>");
out.close();
out.flush();
response.flushBuffer();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Short description";
}
// </editor-fold>
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
ajax asp beta bon browser browsers cross-browser javascript menu with few lines of code developer development echo email encryption europe firefox gecko home html internet internet explorer javascript javascript smooth scrolling scroll smoothly window document position javascript tab menu with rounded corners generator leak linux memory microsoft mozilla msdn news nintendo office open source opera patch privacy scams security site software spoof sql testing thunderbird url users vista web webmail wii windows
- Problem in Ajax (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Change function in bottom
- Next Thread: two scripts beat as one



Linear Mode