[英]PHP/Ajax /JQuery response not working properly
我有index.php文件,如下所示:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="ajax.js"></script>
</head>
<body>
<div id="summary">The returned value will go here</div>
</body>
</html>
應該一直調用/調用function.php的ajax.js的內容,直到function.php返回'1'為止:
function check(){
return $.ajax({
url: 'function.php',
type:'POST',
success: function(response){
if(response == '1'){
$('#summary').html(response);
}else{
check();
}
},
failure: function(jqXHR, textStatus, errorThrown){
console.log(textStatus);
}
});
}
check();
最后是function.php :
<?php
echo "1";
?>
我希望function.php只能被調用一次,因為function.php返回“ 1”。 但是,它不斷不斷地調用function.php 。
有人可以指出我如何使其正常工作嗎?
代碼看起來不錯,但是1個可能的原因是響應中可能有前導或尾隨空格。
一個解決方案可能是在比較之前修剪響應值
if (response.trim() == '1') {//use $.trim(response) if wants to support < IE9 or use a [polyfill](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/trim#Polyfill)
$('#summary').html(response);
} else {
check();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.