javascript 未捕获的类型错误:无法读取未定义的属性“responseText”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16874297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught TypeError: Cannot read property 'responseText' of undefined
提问by Aviel Fedida
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
未捕获的类型错误:无法读取未定义的属性“responseText”
Refused to set unsafe header "Content-length" register.php:1
拒绝设置不安全的标头“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.
即使没有最后 2 个错误(如果我没有设置连接、内容长度标头),我也会得到第一个错误。
For my ajax functionality i am using this function:
对于我的 ajax 功能,我正在使用此功能:
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:
在我的 js 代码中,我正在调用这样的函数:
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:
AjaxLoad 参数说明:
- xhr object
- location
- onreadystatechange handler
- Synchronous(its not working even if i will pass true)
- method
- headers
- key=value pair(I did test it so it get the right value)
- xhr 对象
- 地点
- onreadystatechange 处理程序
- 同步(即使我会通过它也不起作用)
- 方法
- 标题
- 键=值对(我确实测试过,所以它得到了正确的值)
This is the getResponse function:
这是 getResponse 函数:
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(); } } }
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.phpis even get to the browser and i get status 200 everything seems ok.
我测试网络以查看文件captchaValidator.php是否甚至可以访问浏览器,并且我得到状态 200 一切似乎都正常。
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.
我确实测试了正确的值,代码在我不知道发生了什么变化之前确实有效,但这是指向我的网站注册页面链接的链接,如果您按发送,您可以看到(在网络选项卡的 Chrome 浏览器中)好的,但是我收到了我在控制台选项卡中提到的错误,如果有人可以请看一看,我将非常感激。
采纳答案by Bergi
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.
正如错误所说,xhr
变量未定义。快速浏览一下该getResponse
函数,您会发现它可能不在范围内。作为参数的那个是AjaxLoad
该函数的本地参数(顺便说一句,将它作为参数似乎毫无用处,因为您做的第一件事就是分配给它),而您在调用中作为参数传递的那个可能是undefined
或从不同的范围。
Instead, you can access the current XMLHttpRequest
object from the event handler with the this
keyword. Change your code to
相反,您可以使用关键字XMLHttpRequest
从事件处理程序访问当前对象。将您的代码更改为this
if (this.responseText == "0"){
回答by Eric
If you're trying to use ajax and have jQuery, use $.ajax
and friends:
如果您尝试使用 ajax 并拥有 jQuery,请使用$.ajax
和朋友:
$.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.
您的代码不起作用,因为该变量xhr
不在getResponse
. 您可以this
在事件处理程序内部使用,而不是xhr
引用 XHR 对象。