这个问题已经让我有点发疯了几个多小时了。我已经阅读了很多帖子和文章,现在我对这个问题有一个模糊的理解,但不是原因。我意识到问题在于,当我在未定义的对象上提交 JSON.parse 之类的内容时。我不知道为什么会这样。我确信它非常简单,并且我确信一旦它被指出我会感觉到一个工具,但现在我不介意如果它解决了问题,我会觉得有点傻。
问题是,当我使用以下方法提交表单时,出现以下错误:
Uncaught SyntaxError: Unexpected token u js:1
i.extend.parseJSON js:1
c js:1
u js:1
n.extend.showLabel js:1
n.extend.defaultShowErrors js:1
n.extend.showErrors js:1
n.extend.form js:1
n.extend.valid js:1
(anonymous function)
i.event.dispatch js:1
y.handle
此外,当我将光标从一个字段移动到另一个字段时,我收到此错误:
Uncaught SyntaxError: Unexpected token u js:1
i.extend.parseJSON js:1
c js:1
u js:1
n.extend.showLabel js:1
n.extend.defaultShowErrors js:1
n.extend.showErrors js:1
n.extend.element js:1
n.extend.defaults.onfocusout js:1
r js:1
(anonymous function) js:1
i.event.dispatch js:1
y.handle js:1
i.event.trigger js:1
i.event.simulate js:1
f
这显然阻止了我将表单提交给控制器进行处理。错误似乎来自 jquery 库(第 498 行),当它试图: return n.JSON.parse(t); (t 未定义)。我正在使用 jquery 1.9.1、bootstrap 2.2.2 和 jqValidate/1.9.0.unobtrusive.js。该表单处于引导模式,如下所示:
@model AModelTestApp.Models.UserModel
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>User</h3>
</div>
<div class="modal-body">
@{
var ajaxOptions = new AjaxOptions
{
HttpMethod = "POST",
OnComplete = "Complete"
};
}
@using (Ajax.BeginForm("EditUser", "Home", ajaxOptions, new { id = "userform" }))
{
<div class="row-fluid">
<div class="span12">
@Html.ValidationSummary("Ooops..", new { id = "validateuser", @class = "alert alert-error" })
</div>
</div>
<div class="row-fluid">
<div class="span6">
@Html.LabelFor(m => m.Id)
@Html.TextBoxFor(m => m.Id)
@Html.LabelFor(m => m.Username)
@Html.TextBoxFor(m => m.Username)
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
@Html.LabelFor(m => m.LastName)
@Html.TextBoxFor(m => m.LastName)
</div>
<div class="span6">
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email)
@Html.LabelFor(m => m.UserTypeId)
@Html.TextBoxFor(m => m.UserTypeId)
@Html.LabelFor(m => m.Created)
@Html.TextBoxFor(m => m.Created)
@Html.LabelFor(m => m.Active)
@Html.CheckBoxFor(m => m.Active)
</div>
</div>
<div class="row-fluid">
<div class="span1 offset10">
<a id="btnclear" class="btn" href="#">Clear</a>
</div>
</div>
}
</div>
<div class="modal-footer">
<a class="btn" data-dismiss="modal" href="#">Close</a>
<a id="btnsubmit" class="btn btn-info" href="#">Submit</a>
</div>
<script type="text/javascript">
$.validator.unobtrusive.parse($('#userform'));
$('#btnclear').click(function() {
$('input').val('');
$(':checkbox').prop('checked', false);
return false;
});
$('#btnsubmit').click(function () {
$('#userform').validate();
if ($('#userform').valid()) {
$('#userform').submit();
} else {
alert("Something went wrong with the validation")
}
});
function Complete(result) {
alert("Woooo - " + result);
}
</script>
模态加载如下:
<div class="row-fluid">
<div class="span12">
<button id="btnclick" class="btn btn-large btn-block btn-primary">Click Me</button>
</div>
</div>
<div id="mainmodal" class="modal hide fade"></div>
<script type="text/javascript">
$('#btnclick').click(function () {
$.ajax({
url: '@Url.Action("GetUser", "Home")',
type: 'GET',
success: function (result) {
//awss.modal.popup(result);
$('#mainmodal').html(result);
$('#mainmodal').modal('show');
}
});
return;
});
</script>
从这个控制器:
public ActionResult GetUser()
{
var user = new UserModel
{
Id = 1,
Username = "sbody",
FirstName = "Some",
LastName = "Body",
Email = "sbody@domain.com",
UserTypeId = 6,
Created = new DateTime(2013, 1, 1),
Active = true
};
return PartialView("EditUser", user);
}
在 web.config 中启用了 ClientValidationEnabled 和 UnobtrusiveJavaScriptEnabled。我希望这只是我长时间关注同一个问题而看不到明显问题的一个例子。非常感谢任何帮助。谢谢 :)