javascript - if 语句在 ajax 成功时给出相反的结果

标签 javascript php jquery ajax

大家好,我正在使用ajax来检查值是否相同,我正在获取有关ajax成功的真实数据,但是当我使用js的if语句时,警报它给了我相反的结果,请协助-这是我的代码--

index.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">

</script>
<script type="text/javascript">
$(function() {

setInterval("alert()", 5000);

});
function alert() {

      var aval=1;
      $.ajax({
        type: "POST",
        url: "php.php",
        data: 'uid=' + aval,
        success: function(html)
            { // this happen after we get result

                 if (html.success == true)
                 { 
                    alert ('Same Value');
                 }
                 else {
                    alert('Different Value')
                 }

             }
        });
}


</script>
Old Value:1<br>
<div>New Value:</div><div id="new" ></div>

php.php

<?php 
//some php coding which shows result

echo '1';

?>

最佳答案

将您的成功部分更改为:

success: function (html) {
        if (html == '1') { 
            alert('Same Value');
        } else {
            alert('Different Value');
        }
    }

这将检查该值(在本例中为 1)

关于javascript - if 语句在 ajax 成功时给出相反的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32396858/

相关文章:

javascript - 将javascript类方法定义为常量

php - 如何将多行计数与其容器分组

javascript - Jquery 检查复选框是否被选中

javascript - DataTable 水平滚动条看起来像窗口滚动 -x

javascript - 通过 self 时jquery each()索引

javascript - 在 JS 中,为什么我能够使用诸如 toFixed() 之类的函数,它驻留在原始类型的 Number 包装器对象的原型(prototype)中?

javascript - highcharts 定位器工具提示 - 一般 javascript

javascript - 将 csrf token 公开给位于不同域的客户端

php - 将数百万张图像导入、调整大小并上传到 Amazon S3

php - codeigniter 中的 XSS 过滤不能防止 SQL 注入(inject)吗?