[英]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.