DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   Problem with ajax and mozilla browser (http://www.daniweb.com/forums/thread103808.html)

sree22_happy Jan 7th, 2008 6:03 am
Problem with ajax and mozilla browser
 
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....

MidiMagic Jan 8th, 2008 2:48 pm
Re: Problem with ajax and mozilla browser
 
Check to see if any of the methods or functions you are using are nonstandard IE extensions to JS. Other browsers can't use them.

digital-ether Jan 9th, 2008 10:15 am
Re: Problem with ajax and mozilla browser
 
Can't see why your codes doesn't invoke xmlHttp=new XMLHttpRequest(); bit odd.

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

sree22_happy Jan 17th, 2008 5:29 am
Re: Problem with ajax and 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

digital-ether Jan 17th, 2008 6:43 am
Re: Problem with ajax and mozilla browser
 
Quote:

Originally Posted by sree22_happy (Post 512201)
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.

digital-ether Jan 17th, 2008 8:08 am
Re: Problem with ajax and mozilla browser
 
You can also replace:
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.

sree22_happy Jan 18th, 2008 6:19 am
Re: Problem with ajax and mozilla browser
 
thanks and let me check if any problem i will reply soon ?

sree22_happy Jan 23rd, 2008 6:52 am
Re: Problem with ajax and mozilla browser
 
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!");
}
}
}
}
}

sree22_happy Jan 23rd, 2008 6:53 am
Re: Problem with ajax and mozilla browser
 
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>
}

sree22_happy Jan 23rd, 2008 6:55 am
Re: Problem with ajax and mozilla browser
 
Problem is when i run this code in IE it works fine.It takes the second try method in the index.jsp
When it comes to mozilla it throws an exception from the first try method as "Please use a new browser! This browser does not support XML!"

so can u help me please


All times are GMT -4. The time now is 2:24 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC