duanbianweng5353 2016-03-14 06:02
浏览 57
已采纳

ajax调用忽略我的if语句

I have a modal on my page that submits some data via ajax to my Laravel application, which is then validated to make sure a field is present.

If the field isn't present then Laravel will return a json response with an error status.

I am then checking for the status in the success part of my ajax call for this and displaying an error message if this is the case.

However, even though I am returning an error status, it seems my if statement is missing this and continuing on with the statement.

Below is the whole script:

<script>
    $(document).ready(function () {
        $("#addShiftButton").click(function(){
            $.ajax({
                type: "POST",
                url: "calendar/addhours",
                data: {
                    'name':$('#name').val(),
                    'start_time':$('input[name=start_time]').val(),
                    'finish_time':$('input[name=finish_time]').val(),
                    'add_shift_date':$('#add_shift_date').val(),
                    'breaks':$('input[name=breaks]').val(),
                    'shift_status':$('#shift_status').val()
                },
                success: function(data){
                    if (data.status == "error")
                    {
                        $("#error").show();
                    }
                    else
                    {
                        $('#addShiftModal').modal('hide'); //hide popup  
                        //location.reload();
                    }
                },
                error: function(){
                    alert("Error");
                }
            });
        });
    });
</script>

I have checked the call and it is being successfully made and a json response is received like so: {"status":"error"}.

However the else part of my if statement is being executed. Can anyone see why?

  • 写回答

2条回答 默认 最新

  • dongyue110702 2016-03-14 06:07
    关注
    <script>
    $(document).ready(function () {
        $("#addShiftButton").click(function(){
            $.ajax({
                type: "POST",
                url: "calendar/addhours",
                data: {'name':$('#name').val(), 'start_time':$('input[name=start_time]').val(), 'finish_time':$('input[name=finish_time]').val(), 'add_shift_date':$('#add_shift_date').val(), 'breaks':$('input[name=breaks]').val(), 'shift_status':$('#shift_status').val()},
                success: function(data){
                    var result = jQuery.parseJSON( data );  // you need to convert json into javascript array 
                    if (result.status == "error")
                    {
                        $("#error").show();
                    }
                    else
                    {
                        $('#addShiftModal').modal('hide'); //hide popup  
                        //location.reload();
                    }
                },
                error: function(){
                    alert("Error");
                }
            });
        });
    });
    

    basically what you are doing is trying to access the members of a json object via data.status , you have to convert it into a javascript object before doing that as shown in the above code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题