未捕获的类型错误:调用函数时无法读取未定义的属性“then”
Uncaught TypeError: Cannot read property 'then' of undefined when calling a function
我的脚本出了什么问题,我得到了
未捕获的类型错误:无法读取未定义的属性"then"
这是我的脚本:
<script type="text/javascript" src="jquery-2.1.3.min.js"></script>
<script type="text/javascript">
function connected() {
$.ajaxSetup({ cache: false });
$.ajax({
type:"get",
url:"cgi-bin/check",
success:function(data) {
if (data.indexOf("192.168.1.1:1080")>-1) {
var audio = new Audio("on.ogg");
audio.play();
document.getElementById("output").innerHTML = "Connected";
clearTimeout(loop);
}
}
});
loop = setTimeout(connected, 1000);
}
function disconnected() {
$.ajaxSetup({ cache: false });
$.ajax({
type:"get",
url:"cgi-bin/check",
success:function(data) {
document.getElementById("output").innerHTML = "function disconnected ";
if (data.indexOf("ssh disconnected")>-1) {
var audio = new Audio("off.ogg");
audio.play();
document.getElementById("output").innerHTML = "Disconnected: "+data;
clearTimeout(loop);
}
}
});
loop = setTimeout(disconnected, 1000);
}
function notif() {
var loop;
$.ajaxSetup({ cache: false });
$.ajax({
type:"get",
url:"cgi-bin/check",
success:function(data) {
if (data.indexOf("192.168.1.1:1080")>-1) {
document.getElementById("output").innerHTML = "It's connected, waiting to disconnect";
disconnected().then(connected);
}
else {
document.getElementById("output").innerHTML = "It's disconnected, waiting to connect";
connected().then(disconnected);
}
}
});
}
notif();
</script>
<p id="output"></p>
这是通知我我的 ssh 隧道是否断开连接/连接的脚本。每次出现时,它都会播放声音。
除非我错过了什么,否则您的 disconnected
和 connected
函数不会返回任何内容,并且您正在尝试对undefined
值执行.then()
。
.then()
是一种Promise
方法
从您的代码来看,您似乎并不完全了解异步事物的工作原理,因此我建议您阅读一些有关此的资源。
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests
易于理解的"异步事件"定义?
http://rowanmanning.com/posts/javascript-for-beginners-async/
参考: http://api.jquery.com/jquery.ajax/
弃用通知:jqXHR.success()、jqXHR.error() 和 jqXHR.complete() 回调从 jQuery 1.8 开始被弃用。准备 您的代码用于最终删除它们,请使用 jqXHR.done(), jqXHR.fail(), 和 jqXHR.always() 代替。
将代码更改为如下所示:
$.ajax({
type:"get",
url:"cgi-bin/check"}).done(function(data) {} );
您可以使用完成或然后。 然后将捕获错误以及成功响应。
相关文章:
- TypeError:无法读取属性'推'未定义的JavaScript
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 未捕获的类型错误:无法读取属性'删除'的未定义
- AngularJS指令出错-无法读取属性'编译'的未定义
- 看到“;未捕获的类型错误:无法读取属性'weight'未定义的“;尽管按照字面上的指示
- angularjs无法读取未定义的属性then
- 无法读取属性'材料'未定义的Three.js
- 得到"TypeError:无法读取属性'filename'未定义的“;调用“npm start
- Soundcloud api"未捕获的类型错误:无法读取属性'uri'“未定义”;
- JavaScript承诺-无法读取属性'那么'的未定义
- 未捕获的类型错误:无法读取属性'中止'的未定义
- 角度推入数组给出:TypeError:无法读取属性'推'的未定义
- Javascript未捕获类型错误:无法读取属性'0'的未定义
- jQuery控制台错误:无法读取属性'top'的未定义
- TypeError:无法读取属性'findAll'的未定义
- 无法读取属性'errcap'未定义的jqgrid
- 无法读取未定义的属性“rid”
- 未捕获的类型错误: 无法读取未定义的属性“图像”
- 可以't读取'getHTML'未定义的Liferay CK Editor内容
- 在node.js中未定义读取属性错误