Link to home
Start Free TrialLog in
Avatar of jbadia
jbadia

asked on

ajax problem in explorer 7, but not in firefox, inserting records

Hello, I have spent 4 days on my problem:

I have an online add TO Basket page where I see some products in php and MySql database.
Now I've implemented Ajax.
I have an ajax and javascript code that when I submit a product form, inserts a product in my basket (on the right of page.
It works fine in Firefox but not in explorer, and I answer to know WHY???

this is my ajax code:

function objetoAjax(){
      var xmlhttp=false;
      try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
            try {
               xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                  xmlhttp = false;
              }
      }
      if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
            xmlhttp = new XMLHttpRequest();
      }
      return xmlhttp;
}
function enviarDatosEmpleado(){
  divResultado = document.getElementById('resultado');
      id=document.frmempleado.id.value;
      color=document.frmempleado.color.value;
      talla=document.frmempleado.talla.value;
      cantidad=document.frmempleado.cantidad.value;
      marca=document.frmempleado.marca.value;
      artId=document.frmempleado.artId.value;
      pid=document.frmempleado.id.value;
  ajax=objetoAjax();
  ajax.open("POST", "actu_Carrito.php",true);
  ajax.onreadystatechange=function() {
  if (ajax.readyState==4) {
  divResultado.innerHTML = ajax.responseText
  }
  }
  ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  ajax.send("id="+id+"&color="+color+"&talla="+talla+"&cantidad="+cantidad+"pid="+pid+"marca="+marca+"artId="+artId)
}

In this page you can see my web application:

http://www.merc-barcelona.com/proves/mig.php?start=0&marca=27&artId=15&pid=486

When a submit the form I call the function 'enviarDatosEmpleado()' that inserts a cell in a mysql table and show again the basket.

I have this code into a page included into a php.

I  can post the rest of the code for anyone who need it.

Thnks a lot.

Jordi
Barcelona
Avatar of hernst42
hernst42
Flag of Germany image

IE7 has a native XMLHttpRequest object, so you might try to change you code to first check of a XMLHttpRequest. Other AXAJ framworks do the creation like (taken from the xajax-Project):

if ("undefined" != typeof XMLHttpRequest) {
    xajax.tools.getRequestObject = function() {
        return new XMLHttpRequest();
    }
} else if ("undefined" != typeof ActiveXObject) {
    xajax.tools.getRequestObject = function() {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP.4.0");
        } catch (e) {
            xajax.tools.getRequestObject = function() {
                try {
                    return new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e2) {
                    xajax.tools.getRequestObject = function() {
                        return new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    return xajax.tools.getRequestObject();
                }
            }
            return xajax.tools.getRequestObject();
        }
    }
} else if (window.createRequest) {
    xajax.tools.getRequestObject = function() {
        return window.createRequest();
    }
} else {
    xajax.tools.getRequestObject = function() {
        throw { name: 'GetRequestObject', message: 'XMLHttpRequest is not available, xajax is disabled' }
    }
}

>> IE7 has a native XMLHttpRequest object

True, but I have an application that fails when I try to use it; if I revert to the methods shown above - as if IE7 did *not* have built-in XMLHTTP support - then all is well.
Avatar of jbadia
jbadia

ASKER

hernst42:
I have tried your new ajax code and don't go yet.

I remember that I have a first page called mig.php that includes some files like imgGranDetalls.php (where I have the form to send) and carritoV2.php (my basket). If I submit it inserts a new record into the basket. In firefox there's no problem but in IE7 it reloads the page and don't insert nothing.
It seems that don't recognize ajax.
BUT:
If I try to load ONLY the page  imgGranDetalls.php  (where's the form), then it goes right and inserts the record!
perhaps I forgot something.
Do you need that I post the code of tree pages?

Jordi

Have you ever resolved? I have the same problem in IE7, native support fails with permission denied. For me, if I reload the page then it works, it's only on the first page load that it fails.
IE has a limitation it can process not more than 2 simultaneously http requests, the work around for this is to create your xmlHTTP object instances, although you can also encapsulate the requests using an array and send one after the other.  However, here are the guidelines which needs some work around for your purpose.

option 1
xmlhttp=new Array();
for(...){
var l=xmlhttp.length;
xmlhttp[l]=GetXmlHttpObject();
xmlhttp[l].open(...);
xmlhttp[l].onreadystatechange=function(){...};
xmlhttp[l].send(null);
}

option II


//this allows detection of XMLHttpRequest without crashing IE
try { new XMLHttpRequest() } catch(e) { XMLHttpRequest = null }

var xmlHttpReqQueue = new Array()
function requestXML(url) {
      var xmlHttpReq
      if (XMLHttpRequest) {      //Mozilla
            xmlHttpReq = new XMLHttpRequest()
            xmlHttpReq.onload = function () {
                  xmlHttpReqQueue.shift()
                  //send the next request if queue isn't empty
                  if (xmlHttpReqQueue.length > 0)
                        xmlHttpReqQueue[0].send(null)
                  doStuff()
            }
      } else if (window.ActiveXObject) {      //IE
            xmlHttpReq = new ActiveXObject('Msxml2.XMLHTTP')
            xmlHttpReq.onreadystatechange = function () {
                  if (xmlHttpReq.readyState != 4)
                        return
                  xmlHttpReqQueue.shift()
                  //send the next request if queue isn't empty
                  if (xmlHttpReqQueue.length > 0)
                        xmlHttpReqQueue[0].send(null)
                  doStuff()
            }
      }
      xmlHttpReq.open('GET', url, true)
      xmlHttpReqQueue.push(xmlHttpReq)
      //if queue was previously empty, there is no request being sent
      //so send this request immediately
      if (xmlHttpReqQueue.length == 1) {
            xmlHttpReq.send(null)
      }
}

ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial