asp.net-mvc - 非侵入式验证期间parseJSON的语法错误

标签 asp.net-mvc jquery-validate unobtrusive-validation jquery-1.9 jquery-1.10

我的MVC应用程序正在生成以下HTML,该HTML在提交时会导致Javascript语法错误(我未在两个文本框中键入任何内容)。这是生成的HTML和提交处理程序:

<form action="/UrIntake/Save" id="UrIntakeForm" method="post">

    <input data-val="true" data-val-length="The field LastName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The LastName field is required." id="FormSubmitter_LastName" name="FormSubmitter.LastName" type="text" value="" />
    <input data-val="true" data-val-length="The field FirstName must be a string with a maximum length of 50." data-val-length-max="50" data-val-required="The FirstName field is required." id="FormSubmitter_FirstName" name="FormSubmitter.FirstName" type="text" value="" />

    <div id="SubmissionButtons" class="right">
            <input type="button" onclick="SubmitForm()" value="Submit" />
            <input type="button" onclick="CancelForm()" value="Cancel" />
    </div>
</form>

    function SubmitForm() {
        $("#UrIntakeForm").valid();
.
.
.


这是发生语法错误的jQuery代码(v1.9.0)。未定义“数据”,而“返回”行是发生错误的地方:

parseJSON: function( data ) {
    // Attempt to parse using the native JSON parser first
    if ( window.JSON && window.JSON.parse ) {
        return window.JSON.parse( data );
    }


大概,我不必在文本框中输入任何内容(然后应该获得“必填字段”消息)。这是引起错误的原因吗?那没有道理,但我不知道还有什么可能。

最佳答案

原因

这是ASP.NET.MVC程序包中jquery.validate.unobtrusive.js的问题。

从jQuery 1.9开始,parseJSON()的行为已更改,并且undefined值将被视为格式错误的JSON,从而导致您指定了错误。有关更多信息,请参见jQuery 1.9 Core Upgrade Guide



使用jQuery Migrate plugin,它除其他功能外还向jQuery parseJSON()实用程序添加了向后兼容性。



编辑

根据此thread on Microsoft Connect中的官方公告,该问题已在最新版本的框架中得到解决。

自然,正如Andreas Larsen在评论中指出的那样,请确保在升级到新版本后清除服务器端和客户端的所有相关缓存。

关于asp.net-mvc - 非侵入式验证期间parseJSON的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14822540/

相关文章:

jquery - MVC3 ModelClientValidationRule 比较 2 个不同的字段。添加一个必需的标志到

jquery - 在部分 View 中进行不显眼的客户端验证

asp.net - 如何在EF Code First中使用会籍提供者?

asp.net-mvc - 如何将名为 "file[]"的发布数据绑定(bind)到 MVC 模型?

jquery - 如何使用 jquery.validate.min.js 验证具有数组类型名称的输入字段

c# - 要么或要求验证

unobtrusive-validation - 是否可以禁用/覆盖 ScriptResourceMapping 要求?

asp.net - Azure 上的 Web 应用程序和数据库主机

c# - IActionFilter 和 IAuthorizationFilter 的区别

javascript - jQuery 验证 require_from_group 仅验证第一个元素