hurriedly% 2016-07-19 19:10 采纳率: 100%
浏览 4

在ajax中的错误没有被击中

I am using ajax to send values from my view to a controller in my application.

Ajax:

<script type="text/javascript">
$('#SendToController').on('click', function() {
    sendToController();
    return false;
});
function sendToController(){
    var selectedM = $('#Month').val();
    var chosenY = $('#Year').val();
    var chosenC = $('#County').val();
    //alert(chosenY);

    $.ajax({
        url: '/ALog/MReports/Generate',
        data: { 'monthV' : selectedM, 'year' : chosenY, 'county' : chosenC },
        type: 'GET',
        cache: false,
        success: function (data) {
            $("body").html(data);
            $('.dropdown-toggle').dropdown();
        },
        error: function () {
            if (selectedM === "") {
                alert("Error 1!");
            }
            else if (chosenY === "") {
                alert("Error 2!");
            }
            else if (chosenC === "") {
                alert("Error 3!");
            }
        }
    });
}


</script>

Now, on my form i have 3 dropdownlists which I am getting the values from in the ajax code above. The only time the ajax function goes to the error part is if chosenC and/or selectedM is empty, but if chosenY is empty it still goes to success.

I have alerted the value of chosenY when I left it blank to ensure that I am checking for the right value with javascript, either null or empty ("").. I have tried both, and neither work..

Here is the result from when I alerted the chosenY

enter image description here

so from that I thought it was an empty string.. so I am checking for that.. but it doesn't matter because even if it is empty the ajax method is considering that as a success and not an error.. why is that?

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • weixin_33724046 2016-07-19 19:41
    关注

    Thanks to @MarcB I figured this out. It was in fact on the server side, and it wasn't throwing an error because it was using that value to search for records in the database table even though it was empty string.

    So i just converted the string to an int which would throw an exception if year is passed as an empty string.

    Thanks again for the ideas/help!

    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示