[英]$.get is not a function, Synchronous XMLHttpRequest on the main thread is deprecated
我正在为我正在构建的网页编写 JavaScript。 我成功地使用了以下(简化的)代码:
$('#selector').on('click', function () {
if (isTrue) {
if (isTrue) {
}
else {
// execute block of code 'A', which is similar to 'B'
callAjax(url, function() {
// do some things...
})
}
}
else {
// execute block of code 'B', which is similar to 'A'
callAjax(url, function() {
// do some things...
})
}
});
/**************************
* Call Ajax
**************************/
function callAjax(url, callback) {
$.get(
url,
function(data) {
callback();
}
)
}
但是,我想通过像这样模块化来改进代码:
$('#selector').on('click', function () {
if (isTrue) {
if (isTrue) {
}
else {
handler(objects);
}
}
else {
handler(objects);
}
});
/**************************
* Handler
**************************/
function handler(objects) {
callAjax(url, function() {
// do some things...
})
}
/**************************
* Call Ajax
**************************/
function callAjax(url, callback) {
$.get(
url,
function(data) {
callback();
}
)
}
每当我运行第二组代码时,它都会抛出“不推荐使用主线程上的同步 XMLHttpRequest”警告。 随后运行代码会抛出“$.get 不是函数”错误,并且一旦调用 handler(),代码就会停止运行。
我不是指 JavaScript 的超薄版本。
为什么会抛出这些错误和警告,我怎样才能按照我描述的方式成功地简化我的代码?
编辑:
为了增加清晰度并回答我到目前为止的评论,以下是我在网页底部的<body></body>
标签中包含的脚本:
<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
我想我也应该包括这个。 它引用了我正在编写的 .js 文件。
<script src="script.js"></script>
检查我的代码并参考以下链接中的帖子后:
JavaScript console.log 导致错误:“不推荐使用主线程上的同步 XMLHttpRequest...”
我意识到我的代码中有一个错误。 我将“未定义” url 传递给 ajax 请求,而不是我期望的字符串路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.