| | |
Problem with ajax and mozilla browser
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread 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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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
You should use
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var textout = xmlHttp.responseXML;
instead of
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
header('Content-Type: text/xml');
OR you can force the browser to treat the response as XML.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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 7: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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
var x= new DOMParser().parseFromString(textout, 'text/xml'); xmldom.async="false"; xmldom.loadXML(x); alert(xmldom); xmlObj=xmldom.documentElement;
with:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
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>
}
![]() |
Similar Threads
- 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
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element embed enter error events explorer firefox focus frameworks getselection google gwt hint html htmlform ie7 iframe image() images index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp listbox maps masterpage menu microsoft mimic mp4 object onmouseover parent paypal php player position post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle variables w3c webservice website window windowofwords windowsxp wysiwyg






