简体   繁体   English

jquery ajax响应中的if else条件

[英]if else condition in jquery ajax response

I have one Ajax function which is running properly.but i want when my Ajax response is 我有一个可以正常运行的Ajax函数。但是我想当我的Ajax响应是

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

i want to display some error message else i want to display another map div in else condition. 我想显示一些错误消息,否则我想在其他情况下显示另一个地图div。 but every time when i hit Ajax only else condition is working..but when i alert response and see the output it shows this message when 但是每次当我按下Ajax时,只有其他条件起作用。.但是当我警告响应并看到输出时,它会在以下情况下显示此消息

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

but still it not comes in if condition..can anyone help me to do this.... 但如果条件仍然没有出现..任何人都可以帮助我做到这一点....

<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>

In your php script, return a boolean flag instead of a string : 在您的php脚本中,返回一个boolean标志而不是一个字符串:

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

And in the ajax success : 在ajax成功中:

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

Hope it helps. 希望能帮助到你。

PS : use Json Encode to parse the response and access values easily. PS:使用Json Encode轻松解析响应和访问值。

First of all, i suggest you to use status for ajax response something like: 首先,我建议您将status用于ajax响应,例如:

1 for success
0 for failure 

Than, as per your statement, your are getting the correct response in: 比照您的陈述,您将在以下方面获得正确的答复:

alert(response);

Than, you must need to check either response having <h3></h3> tags or not. 然后,您必须检查带有<h3></h3>标签的响应。

In your code, the main issue is that, you are using string without quotes in alert alert(thanks); 在您的代码中,主要的问题是,您正在使用警报中没有引号的字符串alert(thanks); this will return undefined thanks in console and treated as a variable. 这将在控制台中返回undefined thanks ,并被视为变量。

This should be alert("thanks"); 这应该是alert("thanks");

One more suggestion, it's always better to check browser console when you are not getting success in Ajax or any other script, this will help you to find the errors. 还有一个建议,当您在Ajax或任何其他脚本中未获得成功时,最好检查浏览器控制台,这将帮助您发现错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM