【问题标题】:Ajax error: "Syntax error: Unexpected token <"Ajax 错误:“语法错误:意外的令牌 <”
【发布时间】:2014-09-23 11:40:33
【问题描述】:

我正在使用 MVC 5 应用程序,我试图通过 ajax 将数据传递到我的控制器,但我收到“语法错误:意外令牌

这是我的带有 javascript 的 html 页面。

@model webby.Models.Calendar



<div id="calendar">
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="padding:20.5% 15%;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="myModalLabel">Events on @ViewBag.Date</h4>
            </div>
            <div class="modal-body">
                <table>


                        <tr> 
                            <td>
                                @Html.DisplayFor( model=> model.events)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.DisplayFor(model => model.type)
                            </td>
                        </tr>
                        <tr>
                            <td>
                                @Html.DisplayFor(model => model.content)
                            </td>
                        </tr>



                </table>


            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

    <script>
        $(document).ready(function () {

            $('#calendar').fullCalendar({
                height: 170,
                selectable: true,
                editable: true,
                defaultView: 'basicWeek',
                dayClick: function (date, jsEvent, view) {

                    $.ajax(
                    {

                        url: '@Url.Action("Calendar","Home")',
                        type: "POST",
                        data: JSON.stringify({ date: date }),
                        dataType: "json",
                        contentType: "application/json; charset=utf-8",
                        cache: false,
                        success: function (response) {
                            alert(response);
                        },
                        error: function (request, status, error) {
                            alert(error);
                            alert(status);
                            alert(request.responseText);
                        }
                    });

                    $('#myModal').modal();
                }
            });    

        });

    </script> 

这是我的控制器:

    [HttpPost]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public ActionResult Calendar(string date)
    {



        DateTime objDate = Convert.ToDateTime(date);

        var content = db.Calendars.Where(x => x.startDate == objDate).Single();


        return PartialView(content);
    }

我相信这个错误导致数据在通过我的控制器的帖子推送后没有显示在我的视图中。

【问题讨论】:

    标签: c# javascript asp.net ajax asp.net-mvc-5


    【解决方案1】:

    看看返回的是什么

    JSON.stringify({ date: date })
    

    我相信你会在那里找到罪魁祸首。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-28
      • 2019-01-17
      相关资源
      最近更新 更多