Home >Web Front-end >JS Tutorial >jQuery ajax solves the problem of synchronization failure when async is false

jQuery ajax solves the problem of synchronization failure when async is false

高洛峰
高洛峰Original
2016-12-06 14:51:191153browse

jQuery’s ajax, when async is false, the synchronization operation fails. Solution, jQueryasync

recently encountered jquery's Ajax of Jquery. When Async is False, the problem of failed to operate synchronously, search online, and get the solution.

$.ajax({
       url : 'your url',
       data:{name:value},
       cache : false,
       async : true,
       type : "POST",
       dataType : 'json/xml/html',
       success : function (result){
         return result;
       }
     });

Solution:

var ret = null;
$.ajax({
        url : 'your url',
        data:{name:value},
        cache : false,
        async : true,
        type : "POST",
        dataType : 'json/xml/html',
        success : function (result){
          ret=result;
        }
      });
return ret;

Instructions:

Don’t return directly in the success callback function, the specific reasons will be investigated later! !

$ajax() is invalid for setting synchronous submission. The code is as follows. Async: "false" is set but it is still submitted asynchronously.

The original purpose of ajax is to perform asynchronous operations, and the latest jQuery version has even deprecated the async parameter. .

If you just want to use ajax for server interaction and do not need asynchronous refresh effect, you can use the callback function.

jquery $ajax function, async: "false", if it doesn't work, please help

"false" is a string if double quotes are added, and a non-empty string is true.

Remove the double quotes.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn