Question: Why is location.href = "../exit.html";
not executed, but window.location.href = 'http://www.baidu. com';
?
Is there any way to finish executing getData()
. If the data acquisition fails, jump to ../exit.html
and no longer execute gourl();
What about the method?
Supplement:async: false in ajax is a synchronous request! ! !
, this is just a simple demo. In fact, there may be a lot of logic behind the getData() method, but if getData() fails to obtain data, the program will not be allowed to execute other methods, and other methods may not be executed at the same time. inside a file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<p>
<h2>我是测试页面,看我是否发生跳转</h2></p>
<script src="https://cdn.bootcss.com/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
getData();
gourl();
});
function getData() {
var is_success = false;
$.ajax({
type: "GET",
url: "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=bug&bk_length=600",
data: "",
dataType: "json",
async: false,
success: function(data) {
if (data.status == 200) {
is_success = true;
} else {
is_success = false;
}
}
});
if (!is_success) {
location.href = "../exit.html";
}
}
function gourl() {
console.log('我被执行了');
window.location.href = 'http://www.baidu.com';
}
</script>
</body>
</html>
Then you can call back gourl after the success of the getData method to perform the logical processing you want
In addition, I don’t know how to judge your is_success specifically because there is $ajax and a corresponding error
Your code is equivalent to executing the following two sentences:
When these two sentences are executed continuously, they will jump to the following address
My guess is that it takes time for the browser to access the first one, but before it succeeds, the second jump comes again, so I give up the first jump and execute the second jump, similar to quickly entering the address twice in the URL. Same.
The
gourl() function cannot be called in front, but should be placed in the middle of the Ajax logic, and add
after the if logicelse{gourl();}
that is:
The code of the questioner can be understood as follows:
When there are two consecutive location.hrefs in the code, the subsequent jump will be executed. The subject of this question can try it himself.
In addition, since ajax is asynchronous, the subject needs to write
if(!is_success)
into the error in ajax, or into the else judgment in success, otherwise it will jump regardless of whether ajax is successful or not.gourl()
should also be written into success.In addition, cross-domain errors should occur if ajax is used directly like this. It is recommended to use a proxy or other methods to solve cross-domain problems.
Mobile phone code, is this what you mean?
Mainly using jQ’s promises, all written asynchronously, all successful callbacks of ajax are placed in the done of Deferred (if there are multiple, they can also be written as arrays), and then just give a status directly in the done of ajax.
Since
getData
andgourl
have an execution relationship, eithergourl
is placed in the callback judgment. This can be suitable for asynchronous use.If it’s synchronization with the subject, then it’s okay
Can I control Gourl directly here to execute it?
There may be something wrong with your code logic. Ajax is asynchronous. gourl(); This function should not be called in that place. It can be called in the success or failure callback of the ajax request.
Looking at your request, it should be called in success.