Aviel Fedida
Aviel Fedida

Reputation: 4102

Uncaught TypeError: Cannot read property 'responseText' of undefined

I got a register page on my website, The problem is that i started to get this errors:

Uncaught TypeError: Cannot read property 'responseText' of undefined

Refused to set unsafe header "Content-length" register.php:1

Refused to set unsafe header "Connection"

Even without the last 2 errors (if i dont set the connection,content-length headers) i will get the first error.

For my ajax functionality i am using this function:

function AjaxLoad(xhr, url, cfunc, syn, method, headers, params)
{
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    } else {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xhr.onreadystatechange = cfunc; // This line take place on true/false

    xhr.open(method, url, syn);
    $.each(headers ,function(index, value){
        xhr.setRequestHeader(index, value);
    });
    xhr.send(params);
}

At my js code i am calling the function like that:

var headersObject = {"Content-type" : "application/x-www-form-urlencoded", "Content-length" : userCap.val().length, "Connection" : "close" };    

AjaxLoad(
    xhr,
    "../php/Ajax/captchaValidator.php",
    getResponse,
    false,
    "POST",
    headersObject,
    userCap.attr("name") + "=" + encodeURIComponent(userCap.val())
);

AjaxLoad arguments explanation:

  1. xhr object
  2. location
  3. onreadystatechange handler
  4. Synchronous(its not working even if i will pass true)
  5. method
  6. headers
  7. key=value pair(I did test it so it get the right value)

This is the getResponse function:

function getResponse(){
        var strongEl = capCon.next();
            if(xhr.responseText == "0"){
                if(strongEl.prop("tagName") == "STRONG"){
                    strongEl.getReplace('<strong id="sE">קוד שגוי</strong>');
                }else{
                    capCon.after('<strong id="sE">קוד שגוי</strong>').next();
                }
                bToSubmit = false;
            }else{
                if(strongEl.prop("tagName") == "STRONG"){
                    strongEl.remove();
                }
            }
    }

I test the network to see if the file captchaValidator.php is even get to the browser and i get status 200 everything seems ok.

I did test for right values, The code did worked before i dont know what changed, But this is the link to my website registration page Link, If you press send, You can see (in chrome browser at the network tab) that all is ok, But i get the errors i mentioned at the console tab, If some one can please take a look i will be very thankful.

Upvotes: 4

Views: 13598

Answers (2)

Bergi
Bergi

Reputation: 665465

As the error says, the xhr variable is undefined. With a quick glance at the getResponse function you can see that it's probably not in scope. The one that is a parameter to AjaxLoad is local to that function (btw, having it as a parameter seems pretty useless as the first thing you do is assigning to it), and the one which your passing as an argument in your invocation is probably undefined or from a different scope.

Instead, you can access the current XMLHttpRequest object from the event handler with the this keyword. Change your code to

if (this.responseText == "0"){

Upvotes: 2

Eric
Eric

Reputation: 97681

If you're trying to use ajax and have jQuery, use $.ajax and friends:

$.post(
    "../php/Ajax/captchaValidator.php",
    userCap.attr("name") + "=" + encodeURIComponent(userCap.val()),
    function(responseData) {
        //...
    }
)

Your code doesn't work because the variable xhr is not in the scope of getResponse. You can use this inside the event handler instead of xhr to refer to the XHR object.

Upvotes: 1

Related Questions