IE8 está rompiendo mi AJAX ... FF está bien
-
06-07-2019 - |
Pregunta
Sintiéndome muy orgulloso de mí mismo después de crear un formulario con un envío AJAX, lo pruebo en IE8 y obtengo " Mensaje: 'cantidad' no está definida " ;. He leído que podría tener algo que ver con el hecho de que las versiones anteriores de IE usaban ActiveX para solicitudes AJAX, pero soy muy nuevo en JS y no tengo una comprensión real del problema, y ??mucho menos la capacidad de implementar una solución .
Aquí está mi código:
var time_variable;
function getXMLObject() //XML OBJECT
{
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") // For Old Microsoft Browsers
}
catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") // For Microsoft IE 6.0+
}
catch (e2) {
xmlHttp = false // No Browser accepts the XMLHTTP Object then false
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest(); //For Mozilla, Opera Browsers
}
return xmlHttp; // Mandatory Statement returning the ajax object created
}
var xmlhttp = new getXMLObject(); //xmlhttp holds the ajax object
function ajaxFunction() {
var getdate = new Date(); //Used to prevent caching during ajax call
if(xmlhttp) {
var txtname = document.getElementById("txtname");
xmlhttp.open("POST","slots.php",true); //calling testing.php using POST method
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("quantity=" + quantity.value + "&price=" + price.value + "&slot=" + slot.value + "&store=" + store.value); //Posting txtname to PHP File
}
}
function handleServerResponse() {
if (xmlhttp.readyState == 4) {
if(xmlhttp.status == 200) {
document.getElementById("message").innerHTML=xmlhttp.responseText; //Update the HTML Form element
}
else {
alert("Error during AJAX call. Please try again");
}
}
}
Solución
Desde su último comentario sobre su pregunta, sospecho que no está definiendo 'cantidad' en ninguna parte y suponiendo que hará referencia al campo del formulario. Prueba esto:
if(xmlhttp) {
var txtname = document.getElementById("txtname");
var quantity = document.getElementById("quantity");
var price = document.getElementById("price");
var store = document.getElementById("store");
xmlhttp.open("POST","slots.php",true); //calling testing.php using POST method
xmlhttp.onreadystatechange = handleServerResponse;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send("quantity=" + quantity.value + "&price=" + price.value + "&slot=" + slot.value + "&store=" + store.value); //Posting txtname to PHP File
}
Otros consejos
Si la cantidad es un campo de formulario, debe obtenerlo usando getElementById antes de usarlo como lo hizo con txtname:
var quantity = document.getElementById("quantity");
No puede usarlo directamente desde el formulario.