admin管理员组文章数量:1335401
I'd like to know why I get this error if I send a ajax request like:
$.ajax({
url: "testOperation.php",
async: false,
data: pareHeure,
success: function (data, statusRequest) {
})
error:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check /.
Is there a problem ?
Thanks,
I'd like to know why I get this error if I send a ajax request like:
$.ajax({
url: "testOperation.php",
async: false,
data: pareHeure,
success: function (data, statusRequest) {
})
error:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg/.
Is there a problem ?
Thanks,
Share Improve this question edited Jun 21, 2016 at 8:02 Jai 74.7k12 gold badges76 silver badges104 bronze badges asked Jun 21, 2016 at 7:59 GaetanGaetan 662 silver badges8 bronze badges 2- 2 Why do you need a Synchronous ajax? and that's warning not error. – Jai Commented Jun 21, 2016 at 8:00
- 1 It's clearly stating Synchronous XMLHttpRequest thread is deprecated – Justinas Commented Jun 21, 2016 at 8:05
6 Answers
Reset to default 3I'd like to know why I get this error
The short answer is because of this option in the ajax:
async: false,
There could be other better solution to the problem you currently have. If you post more details about why do you need to use Synchronous ajax?
Either use this way:
$.ajax({
url:'',
type:'post',
success:function(data){
// make another ajax call here then
}
})
You can use jQuery.when()
:
$.when( $.ajax("/page1.php"), $.ajax("/page2.php") ).done(function(a1, a2) {
// here you can get both ajax response when both gets pleted.
console.log(a1);
console.log(a2);
});
Here .done()
only executed when both ajax gets pleted.
Just like everyone else said, Synchronous XMLHttpRequest thread is deprecated.
I think you are avoiding Async because you might be waiting for the call to get finish and then do something else. You could instead use a callback like .done
:
$.ajax({
url: "testOperation.php",
data: pareHeure,
success: function (data, statusRequest) {
}
}).done(function(data){
// Do another Ajax or something with the data returned from the Ajax call
})
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg/.
This is just a warning generated which has nothing to do with non working of you code
You have a syntax error in code and missing a }
$.ajax({
url: "testOperation.php",
async: false,
data: pareHeure,
success: function (data, statusRequest) {
}
});
You get a warning because Synchronous XMLHttpRequest block the UI threat and it is generally not a good choice (in fact it has been deprecated).
Blocking the UI threat make any user interaction delayed and sloppy possibly causing the browser to hang for a while.
Wherever possible use AJAX with async calls and use callback or a promise to detect when the call is has status: success, progress or rejected (fail).
Examples:
https://jsfiddle/sb11heds/
var jqxhr = $.ajax( "https://fiddle.jshell/favicon.png" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "plete" );
});
jqxhr.always(function() {
alert( "second plete" );
})
Synchronous AJAX calls are deprecated, because they may produce some issues in page content load.
It may also cause distortion in view-able design, so it is not good practice to use synchronous AJAX, and it may not available in future.
Check if your AJAX call is set to false (async: false) explictly (default is true - which is the right way to go about it), if not then the chances are that you have some extensions running on your browser that do a synchronous call back. Easy way to identify is to open the console in Chrome, enable "Log XMLHttpRequests" and you will be able to see the extension that is causing the issue to be reported. Hope this helps.
本文标签: javascriptjQuery Ajax request async falseStack Overflow
版权声明:本文标题:javascript - jQuery Ajax request async false - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742388878a2465589.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论