Analysis of Get post synchronization request problem in jquery with Ajax

Source: Internet
Author: User

Today you need to use the Ajax get method, encapsulated into its own custom function name, inside but found a problem, always will be the custom of the method inside the whole execution of the code to execute the GET, which led to a different step, which gives us programmers with great inconvenience,

Get when no synchronization code is used

Function name () {

ret = ' Y ';

$.get ("xx.php", function (data) {
ret = data;
});

return ret;

}

The resulting result is always not y and then it gets the things inside the GET request, which creates a different step, and you can use the following code

Set the synchronization global variables for Ajax

$.ajaxsetup ({
Async:false
});

This will be synchronized, but I tested it, and some of my code conflict, resulting in the full CPU, or can be another way

$.ajax ({
Type: "Get",
URL: "url",
Async:false,//Open Sync
Success:function (R) {

The code you're going to execute
}
});

Example

<%@ page language= "java" pageencoding= "Utf-8"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>JQuery</title>
<mce:script type= "Text/javascript" src= "/script/jquery-1.4.2.js" mce_src= "Script/jquery-1.4.2.js" &GT;&LT;/MCE: Script>
<mce:script type= "Text/javascript" ><!--
/** Synchronous Ajax request, in a method body, the function of the method body relies on the AJAX response result * *
function loadsomething ()
{
var condition= "";
$.ajax ({
async:false;//using synchronized AJAX requests
Type: "POST",
URL: "some.php",
Data: "Name=john&location=boston",
Success:function (msg) {
condition=msg;
}
});
/**
* Deal with condition here
* If used asynchronously, perhaps the AJAX request does not respond to the client, but is dropped by the code below the Ajax
* So it could be a problem
*/
}

--></mce:script>
<body>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.