如果jquery中有其他条件,ajax响应


if else condition in jquery ajax response

我有一个正常运行的Ajax函数。但我想当我的Ajax响应是

<h3>No Couriers found near by you.please select another location</h3>

我想显示一些错误信息,否则我想在else条件下显示另一个映射div。但是每次当我点击Ajax时,只有else条件是工作的,但是当我警报响应并看到输出时,它显示这个消息

<h3>No Couriers found near by you.please select another location</h3>

但仍然没有出现在如果条件..谁能帮我做这个....

<script>
$('#weight0,#weight1,#weight2,#weight3').click(function() {
    var checked = $(this).is(':checked');
     if($(this).is(":checked")) { 
        $.ajax({
          type: "POST",
          url: '<?php echo Router::url(array("controller" => "Orders","action" => "searchCourier")); ?>',
          data: {
              frmlat: $("#PoolLatitude").val(),
              frmlong: $("#PoolLongitude").val(),
              mylocation: $("#PoolLocation").val()
          },
          dataType: "html",
          success: function(response) {  
          alert(response);
            if(response =="<h3>No Couriers found near by you.please select another location</h3>"){
              alert(thanks);
            } else {
                $('#map_canvas').css('display', 'none');//used to hide map after ajax success response.
                $("#load_map").html(response); 
            }
          }, 
          complete: function() {
              $('.spinicon').hide();
          }
        });
    } else {
            $("#secretcode").val("");
        }    
});
</script>

在php脚本中,返回一个布尔标志而不是字符串:

<?php
if (some_condition) {
    $return = true;
} else {
    $return = false;
}
die(json_encode(array('return' => $return)));

和在ajax成功:

...
dataType: 'json',
success: function(data) {
    if (data.return) {
        alert("return is true");
    } else {
        alert("return is false");
    }
},
...

希望有帮助。

PS:使用Json Encode解析响应并轻松访问值

首先,我建议您使用ajax响应状态,如:

1 for success
0 for failure 

那么,根据你的陈述,你得到了正确的回答:

alert(response);

那么,您必须检查响应是否有<h3></h3>标签。

在你的代码中,主要问题是,你在警告alert(thanks);中使用没有引号的字符串,这将在控制台上返回undefined thanks并将其视为变量。

应该是alert("thanks");

还有一个建议,当您在Ajax或任何其他脚本中没有获得成功时,最好检查浏览器控制台,这将帮助您找到错误