不引人注目的验证过程中使用parseJSON语法错误 [英] Syntax error with parseJSON during unobtrusive validation

查看:140
本文介绍了不引人注目的验证过程中使用parseJSON语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MVC应用程序正在生成下面的HTML这将导致在提交一个Javascript语法错误(我不会打字任何东西到两个文本框)。下面是生成的HTML和提交处理程序:

My MVC app is generating the following HTML which causes a Javascript syntax error upon submission (I'm not typing anything into the two text boxes). Here's the generated HTML and the submit handler:

<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的code,其中的语法错误发生(v1.9.0)。 数据是不确定的,回归行是哪里发生错误:

This is the jQuery code where the syntax error is occurring (v1.9.0). "data" is undefined and the "return" line is where the error occurs:

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

presumably,我不必输入任何内容到文本框(然后应该得到是必填字段的消息)。这是什么造成的错误?这没有什么意义,但我不认为它可能是什么。

Presumably, I don't have to enter anything into the text boxes (and should then get the "field is required" message). Is this what's causing the error? That doesn't make sense, but I don't see what else it could be.

推荐答案

原因

这是 jquery.validate.unobtrusive.js 的问题在你的ASP.NET.MVC包。

This is an issue with jquery.validate.unobtrusive.js in your ASP.NET.MVC package.

在jQuery 1.9, parseJSON的()的行为已发生变化,一个未定义值将被视为畸形的JSON,致使您所指定的错误。请参阅<一个href=\"http://jquery.com/upgrade-guide/1.9/#jquery-ajax-returning-a-json-result-of-an-empty-string\">jQuery 1.9核心升级指南以获取更多信息。

As of jQuery 1.9, the behavior of parseJSON() has changed and an undefined value would be considered a malformed JSON, resulting in the error you've specified. See the jQuery 1.9 Core Upgrade Guide for more information.

解决方案

使用 的jQuery插件迁移 ,除其他外增加了向后兼容到jQuery的 parseJSON()工具。

修改

根据这个官方公布的thread微软连接这个问题已经在框架的最新版本中解决

According to the official announcement in this thread on Microsoft Connect, the issue has been resolved in the latest release of the framework.

当然,如安德烈亚斯·拉森在评论中指出,一定要清除任何相关的缓存,服务器端和客户端,升级到新版本之后。

Naturally, as Andreas Larsen noted in the comments, make sure to clear any relevant cache, server-side and client-side, after upgrading to the new release.

这篇关于不引人注目的验证过程中使用parseJSON语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆