parseJSON Uncaught SyntaxError:意外的令牌u

Tik*_*keb 9 jquery json asp.net-mvc-4

这个问题一直让我疯狂了几个小时.我现在阅读了很多帖子和文章,我对这个问题有一个模糊的理解,但不是原因.我意识到问题是当我在一个未定义的对象上提交类似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
Run Code Online (Sandbox Code Playgroud)

此外,当我将光标从一个字段移动到另一个字段时,我收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

这显然阻止我将表单提交给控制器进行处理.当它试图:返回n.JSON.parse(t)时,错误似乎来自jquery库(第498行).(t未定义).我正在使用jquery 1.9.1,bootstrap 2.2.2和jqValidate/1.9.0.unobtrusive.js.表单采用bootstrap模式,如下所示:

@model AModelTestApp.Models.UserModel

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</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>
Run Code Online (Sandbox Code Playgroud)

模态加载如下:

<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>
Run Code Online (Sandbox Code Playgroud)

从这个控制器:

    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);
    }
Run Code Online (Sandbox Code Playgroud)

在web.config中启用了ClientValidationEnabled和UnobtrusiveJavaScriptEnabled.我希望这只是我长时间看同一问题并且看不到明显问题的情况.任何帮助非常感谢.谢谢 :)

V2S*_*eam 1

您可以尝试以下操作吗

'$.parseJSON(result)' 
Run Code Online (Sandbox Code Playgroud)

在 ajax 成功函数中使用 parsejson 。