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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器