4

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.

4

2 回答 2

2

正如错误所说,xhr变量未定义。快速浏览一下该getResponse功能,您会发现它可能不在范围内。作为参数的那个是该AjaxLoad函数的本地参数(顺便说一句,将其作为参数似乎毫无用处,因为您要做的第一件事就是分配给它),而您在调用中作为参数传递的那个可能是undefined或从不同的范围。

相反,您可以使用关键字XMLHttpRequest从事件处理程序访问当前对象。将您的代码更改为this

if (this.responseText == "0"){
于 2013-06-01T15:49:21.317 回答
1

如果您尝试使用 ajax 并拥有 jQuery,请使用$.ajax朋友

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

您的代码不起作用,因为该变量xhr不在getResponse. 您可以this在事件处理程序内部使用而不是xhr引用 XHR 对象。

于 2013-06-01T15:37:31.950 回答