[JavaScript] Ajax handleResponse kommt nicht in ReadyState

Dieses Thema im Forum "Webentwicklung" wurde erstellt von SurTana, 15. April 2011 .

Schlagworte:
  1. 15. April 2011
    Ajax handleResponse kommt nicht in ReadyState

    Hi Leute,

    habt Ihr eine Idee, wieso bei mir der Teil nach der ReadyState Prüfung nicht funktioniert?

    Code:
     function checkMenge([COLOR="Red"]id[/COLOR]) {
     try {
     req2 = window.XMLHttpRequest?new XMLHttpRequest(): 
     new ActiveXObject("Microsoft.XMLHTTP"); 
     } catch (e) {
     //Kein AJAX Support
     } 
     [COLOR="Red"]var tid=id;[/COLOR]
     
     req2.onreadystatechange = handleResponse2([COLOR="Red"]tid[/COLOR]);
     menge = document.getElementById([COLOR="Red"]tid[/COLOR]).value;
     
     req2.open('get', 'checkMenge.cfm?id='+ tid + '&menge='+menge);
     req2.send(null); 
     }
     
     function handleResponse2([COLOR="Red"]id[/COLOR]) {
     [COLOR="Red"]alert('222'+id);[/COLOR]
     if ((req2.readyState == 4) && (req2.status == 200)) { 
     [COLOR="Red"][B]alert('111'+id);[/B][/COLOR] //[B]<--- [U]wird nicht angezeigt[/U][/B]
     if(req2.responseText == 1) {
     alert("Sie haben die Zulässige Maximalmenge überschritten");
     }
     else {
     updatePreisG([COLOR="Red"]id[/COLOR]);
     }
     
     } 
     }
    

    aus firebug bekomme ich folgendes:
    Code:
    readyState
     4
     
    responseText
     "\n1\n "
     
    responseXML
     null
     
    status
     200
    
    alert('222'+id); wird angezeigt.

    Aber warum wird der alert('111'+id); nicht angezeigt??
    was mache ich falsch?

    Gruß
    SurTana
     
  2. 15. April 2011
    AW: Ajax handleResponse kommt nicht in ReadyState

    Code:
    req2.onreadystatechange = handleResponse2(tid);
    onreadystatechange erwartet einen callback.

    so sollte es funktionieren:
    Code:
    req2.onreadystatechange = function() { handleResponse2(tid) };
    ich würde deine anfrage so schreiben:
    Code:
    var observe = (function() {
     var e = document.createElement("span");
     var ie = typeof e.attachEvent == "function";
     
     e = null;
     
     if (ie)
     return function(obj, event, fn, c) { obj.attachEvent("on" + event, fn, c); };
     
     return function function(obj, event, fn, c) { obj.addEventListener("on" + event, fn, c); };
    })();
    
    function bind() {
     var args = [];
     
     for (var i = 0, l = arguments.length; i < l; ++i)
     args.push(arguments[i]);
     
     var fn = args.shift(),
     cx = args.shift();
     
     return function() {
     for (var i = 0, l = arguments.length; i < l; ++i)
     args.push(arguments[i]);
     
     fn.apply(cx, args);
     }
    }
    
    function fetch(id) {
     return document.getElementById(id);
    }
     
    function checkMenge(id) {
     try {
     var xhr = new XMLHttpRequest();
     } catch(e) {
     alert("bitte besorge dir einen aktuellen browser");
     return;
     }
     
     observe(xhr, "load", bind(handleResponse, null, xhr, id), false);
     
     xhr.open("GET", "checkMenge.cfm?id=" + id + "&menge=" + fetch(id).value, true);
     xhr.send();
    }
    
    function handleResponse(xhr, id) {
     if (xhr.status == 200) {
     if (xhr.responseText == "1") {
     alert("fehler");
     } else {
     updatePreisG(id);
     }
     } else {
     alert("fehler: " + xhr.status);
     }
    }
    dann bist du nicht mehr von einem bestimmten context abhänig.
     
  3. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.