Foros del Web

Foros del Web (http://www.forosdelweb.com/)
-   Frameworks JS (http://www.forosdelweb.com/f127/)
-   -   Problemas con ajax (http://www.forosdelweb.com/f127/problemas-con-ajax-383069/)

Wdeah 02/04/2006 19:42

Problemas con ajax
 
hola, ando teniendo unos inconvenientes con el temita.. es algo que no entiendo. voy a postear un codigo para que todos vean que esto funciona.

Código:

<script type="text/javascript">
var agt = navigator.userAgent.toLowerCase();
var is_op = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_op;
var is_ie5 = (agt.indexOf("msie 5") != -1) && document.all && !is_op;

function CreateXmlHttpReq(handler) {
  var xmlhttp = null;
  if (is_ie) {
    var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
    try {
      xmlhttp = new ActiveXObject(control);
      xmlhttp.onreadystatechange = handler;
    } catch (ex) {
      alert("You need to enable active scripting and activeX controls"); 
    }

  } else {
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onload = handler;
    xmlhttp.onerror = handler;
  }
  return xmlhttp;
}


function XmlHttpPOST(xmlhttp, url, data) {
  try {
    xmlhttp.open("POST", url, true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlhttp.send(data);

  } catch (ex) {
    // do nothing
  }
}


var myxmlhttp;
ratingMsgs = new Array(6);
ratingMsgColors = new Array(6);
barColors = new Array(6);

ratingMsgs[0] = "Demasiado corta";
ratingMsgs[1] = "Débil";
ratingMsgs[2] = "Justo";
ratingMsgs[3] = "Correcto";
ratingMsgs[4] = "Óptima";
ratingMsgs[5] = "Sin clasificar";

ratingMsgColors[0] = "#808080";
ratingMsgColors[1] = "#da5301";
ratingMsgColors[2] = "#ccbe00";
ratingMsgColors[3] = "#1e91ce";
ratingMsgColors[4] = "#1e91ce";
ratingMsgColors[5] = "#808080";

barColors[0] = "#e0e0e0";
barColors[1] = "#da5301";
barColors[2] = "#f0e54b";
barColors[3] = "#1e91ce";
barColors[4] = "#1e91ce";
barColors[5] = "#e0e0e0";

function CreateRatePasswdReq () {
var passwd = getElement('Passwd').value;
var email = getElement('Email').value;
var lastname = getElement('LastName').value;
var firstname = getElement('FirstName').value;
var min_passwd_len = 4;

if (passwd.length < min_passwd_len) {
        DrawBar(0);
}
else {
        passwd = escape(passwd);
        var params = 'Passwd='+passwd+'&Email='+email+'&FirstName='+firstname+'&LastName='+lastname;
        myxmlhttp = CreateXmlHttpReq(RatePasswdXmlHttpHandler);
        XmlHttpPOST(myxmlhttp, "https://www.google.com/accounts/RatePassword", params);
}
}

function getElement(name) {
if (document.all) {
        return document.all(name);
}
return document.getElementById(name);
}

function RatePasswdXmlHttpHandler() {
if (myxmlhttp.readyState != 4) {
 return;
}
rating = parseInt(myxmlhttp.responseText);
DrawBar(rating);
}

function DrawBar(rating) {
var posbar = getElement('posBar');
var negbar = getElement('negBar');
var passwdRating = getElement('passwdRating');
var barLength = getElement('passwdBarDiv').width;

if (rating >= 0 && rating <= 4) {
        posbar.style.width = barLength / 4 * rating;
        negbar.style.width = barLength / 4 * (4 - rating);
}
else {
        posbar.style.width = 0;
        negbar.style.width = barLength;
        rating = 5;
}
posbar.style.background = barColors[rating]
passwdRating.innerHTML = "<font color='" + ratingMsgColors[rating] +"'>" + ratingMsgs[rating] + "</font>";
}
</script>

<table width="100%"  border="0">
        <tr>
                <td width="30%">
<input type="hidden" value="" id="FirstName" size="30">
<input type="hidden" value="" id="LastName" size="30">
<input type="hidden" id="Email" value="" size="30">
<input type="password" id="Passwd" value="" onkeyup="CreateRatePasswdReq()" size="30">
</td>
<td width="70%">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td width="200" id="passwdBarDiv">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td nowrap valign="top"><font color="#808080" size="-1" face="Arial, sans-serif"><div id="passwdRating">Introducir</div></font></td>
</tr>
<tr>
<td height="3"></td>
</tr>
<tr>
<td colspan="2">
<table cellpadding="0" bgcolor="#ffffff" cellspacing="0" border="0" width="200">
<tr>
<td width="0%" id="posBar" bgcolor="#e0e0e0" height="4"></td>
<td width="100%" id="negBar" bgcolor="#e0e0e0" height="4"></td>
</tr>
</table>
</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
</table>

copien ese codigo y guardenlo en formato html, despues ejecutenlo y van a ver que funciona todo joya! sin problemas.
el tema es que cuando lo quiero implementar en mi web, me dice Acceso denegado.
es por el https del orto.. ya lo se, pero no entiendo porque aca funciona
pienso que es porque cuando ejecuto el archivo html, este no esta funcionando como pagina web, sino como un simple archivo, en cambio en mi localhost si..
gracias.

califa010 03/04/2006 17:54

Sí, por ahí anda la cosa. El objeto XMLHttpRequest tiene una restricción de dominio (por cuestiones de seguridad que no me quedan 100% claras, la verdad). El tema es que si la página que tiene este código está en www.sitio1.com, no va a poder comunicarse con www.sitio2.com, ni ningún otro dominio salvo ése en el que está corriendo. O sea que si hacés un post a google, sea o no sea https, no se va a conectar, y no hay tu tía.

Una manera relativamente simple de solucionarlo es crear un "proxy". La idea es simple. Vos no podés comunicarte directamente con otro dominio, pero sí podés comunicarte con un script, pongamos en php, que esté en tu mismo dominio. Entonces es ése script el que se encarga de hacer el pedido al otro dominio, recibe la respuesta y se la devuelve a tu aplicación en AJAX. Para la aplicación, no hay nada raro, porque siempre se está comunicando con "algo" que está en el mismo dominio, que es justamente es script que te hace de "puente".

Suerte
Califa


La zona horaria es GMT -6. Ahora son las 23:05.

Desarrollado por vBulletin® Versión 3.8.7
Derechos de Autor ©2000 - 2026, Jelsoft Enterprises Ltd.