And under the quiet,想了一下AJAX mechanism, the reasons that are up in the mechanism. With colleagues to discuss the issue, the question to determine where the. Debug then the results come out. So share.
First of all, the following are common to send a general request for some code XMLRequest
var _bool = false;
request.open("POST", url, true);
request.onreadystatechange = function callBack(){
var response = request.responseText;//假设response等于true
_bool= response;
/*调试1*/
}
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
request.send(parameters);
/*调试2*/
/*后续操作*/
if(_bool){
//对应的操作
}else{
//对应的操作
} Although the follow-up operation can be placed directly inside callBack, but now let us do so. Is in fact no problem.
Boring response operation the value of the results of the back are any, have a return false. Are beginning to think that the assignment, or the role of problem variables. Under reflection of the errors do not think there is.
Debugging and debug at one position 2, respectively, add debugging alert.
Are the order of 1 → debug debug 2
Information, respectively, are true and false. Feel strange.
Easily be forgotten ------- AJAX mechanism of synchronous and asynchronous synchronous asynchronous
Synchronous and asynchronous, to sum up their experience:
1, if it is asynchronous, then request the attention of some cases will be covered. That is, the first asynchronous client did not return requests for services, go out a second submission. Request the use of the same object, then the first has been covered. So never get a return on the only data. So asynchronous circumstances, does not recommend the use of relatively good overall request. However, request parameters required囖Chuan. And the use of a callback function when completed initialization remember you.
2, in the case of asynchronous, JavaScript code browser are all running directly completed. AJAX back end services then the response should be who is going to receive? The beginning of a lot of people have this question, very easy to mistakenly believe that JavaScript are to receive treatment for then.
In fact, JavaScript code execution, the rest of the mission to the browser. A code translator unknown. Put it, such as long list of boring html code into a variety of fonts, patterns, styles to viewers.
In the case of synchronization, XMLRequest request, the follow-up of the JavaScript code will not continue to carry out, such as the browser will continue after the response. This period of time inside, visitors also should not be used for other operations.
In fact, services and AJAX client interaction time very quickly, basically synchronous and asynchronous, ordinary differential where it is very difficult to debug. Service carried out in the end unless you sleep this operation.
In fact Here, the crux of the problem before there should be a clue. Response can not be the reason why access is due to receive the response after the fact, equivalent to a single individual has. That is the essence of AJAX, asynchronous requests for the role.
So even if the order is that we want, that is alert to debug one location, then debug 2. However, the value of or can not access.
Above code, as long as the
request.open ( "POST", url, true);
Replaced by the true inside false. The result is that we want the implementation.
About asynchronous and synchronous, we must remember the difference between them, and then to flexible use. Although many cases are not any differences exist. However, in continuous development process, they may be the nuances of the procedure you have a very big impact.







