我正在使用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">×</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);
}
我相信这个错误导致数据在我的控制器的帖子中被推后不会显示在我的视图中。
答案 0 :(得分:0)
看一下
返回的内容JSON.stringify({ date: date })
我相信你会在那里找到罪魁祸首。