JQuery Ajax方法不会将成功数据作为Array提交

时间:2021-09-13 19:35:27

I have Jquery Ajax function to get data from MVC controller. Ajax method call perfectly but not in the correct area. Controller method hit after the success method in Ajax call. I'm returning Array from controller to ajax method. But it is not working. when I'm set the alert in success it will popup. i cant assign array value to define variables. Pls help me to sort out this.

我有Jquery Ajax函数从MVC控制器获取数据。 Ajax方法调用完美但不在正确的区域。在Ajax调用中成功方法之后命中控制器方法。我将Array从控制器返回到ajax方法。但它没有用。当我成功设置警报时,它会弹出。我不能分配数组值来定义变量。请帮我解决这个问题。

Jquery Ajax Method:

Jquery Ajax方法:

<script type="text/javascript">
        $(document).ready(function () {
            debugger;
            var austDay = new Date();
            var currDay = new Date();
            $.ajax({
                type: 'GET',
                url: '/Service/Utility/GetDownDate',
                success: function (countDownDetail) {
                    if (countDownDetail.Result.length > 0) {
                        austDay = countDownDetail.Result[0];
                        currDay = countDownDetail.Result[1];
                    }
                }
            });

            austDay = new Date(austDay, 1 - 1, 26);



            $('#defaultCountdown').countdown({ until: austDay });
            $('#year').text(austDay.getFullYear());
        });
</script>

This is MVC Controller Method:

这是MVC控制器方法:

public ActionResult GetDownDate()
    {
        string dtime = db.Maintanance.FirstOrDefault().Time;
        string[] time = dtime.Split(':');

        //int hours = db.Maintanance.FirstOrDefault().date.Hour+;
        DateTime downDate = db.Maintanance.FirstOrDefault().date;
        int minutes = db.Maintanance.FirstOrDefault().duration + Convert.ToInt32(time[1]);

        DateTime newDate = new DateTime(downDate.Year, downDate.Month, downDate.Day, Convert.ToInt32(time[0]), minutes, 0);
        DateTime currentDate = System.DateTime.Now;

        DateTime[] dateTimes = new DateTime[]
        {
            newDate,currentDate
        };

        return Json(new
        {
            Value = dateTimes

        }, JsonRequestBehavior.AllowGet
            );
    }

Thank You,,

3 个解决方案

#1


0  

Whenever you are returing something to Ajax sucess, you need to use rertun JSON not return view like below code

每当你向Ajax成功撤回一些东西时,你需要使用rertun JSON而不是像下面的代码那样返回视图

 public ActionResult GetDownDate()

    {
    .............

    DateTime downDate = db.Maintanance.FirstOrDefault().date;
                int minutes = db.Maintanance.FirstOrDefault().duration + Convert.ToInt32(time[1]);

                DateTime newDate = new DateTime(downDate.Year, downDate.Month, downDate.Day, Convert.ToInt32(time[0]), minutes, 0);
    return Json(new { date = newDate }, JsonRequestBehavior.AllowGet);
    }

in success function like

在成功的功能

success: function (data) {
//data will contain date objects
}

#2


0  

get hold of the HTTP response object and use the write method.

获取HTTP响应对象并使用write方法。

response.getWriter().write(View(newDate));

Hope this helps.

希望这可以帮助。

#3


0  

Return string instead of array using function like implode() with some seperator and split the result using split function in javascript.

使用像implode()这样的函数和一些分隔符返回字符串而不是数组,并使用javascript中的split函数拆分结果。

 success: function (countDownDetail) {
    var countDownResult=countDownDetail.split(seperator);
    if ( countDownResult.length > 0) {
        austDay =  countDownResult[0];
        currDay =  countDownResult[1];
        }
    }

#1


0  

Whenever you are returing something to Ajax sucess, you need to use rertun JSON not return view like below code

每当你向Ajax成功撤回一些东西时,你需要使用rertun JSON而不是像下面的代码那样返回视图

 public ActionResult GetDownDate()

    {
    .............

    DateTime downDate = db.Maintanance.FirstOrDefault().date;
                int minutes = db.Maintanance.FirstOrDefault().duration + Convert.ToInt32(time[1]);

                DateTime newDate = new DateTime(downDate.Year, downDate.Month, downDate.Day, Convert.ToInt32(time[0]), minutes, 0);
    return Json(new { date = newDate }, JsonRequestBehavior.AllowGet);
    }

in success function like

在成功的功能

success: function (data) {
//data will contain date objects
}

#2


0  

get hold of the HTTP response object and use the write method.

获取HTTP响应对象并使用write方法。

response.getWriter().write(View(newDate));

Hope this helps.

希望这可以帮助。

#3


0  

Return string instead of array using function like implode() with some seperator and split the result using split function in javascript.

使用像implode()这样的函数和一些分隔符返回字符串而不是数组,并使用javascript中的split函数拆分结果。

 success: function (countDownDetail) {
    var countDownResult=countDownDetail.split(seperator);
    if ( countDownResult.length > 0) {
        austDay =  countDownResult[0];
        currDay =  countDownResult[1];
        }
    }