1

我的视图上有 ajax 表单,但是当我尝试提交它时出现以下错误: Opera 12.16控制台输出:

Error thrown at line 541, column 3 in <anonymous function: parseJSON>(data) in http://mysite/Scripts/jquery-1.9.1.js:
    return window.JSON.parse( data );
called from line 33, column 2 in onError(error, inputElement) in http://mysite/Scripts/jquery.validate.unobtrusive.js:
    var container = $(this).find("[data-valmsg-for='" + inputElement[0].name + "']"),
called via Function.prototype.apply() from line 818, column 3 in <anonymous function: proxy>() in http://mysite/Scripts/jquery-1.9.1.js:
    return fn.apply( context || this, args.concat( core_slice.call( arguments ) ) );
called from line 619, column 7 in <anonymous function: showLabel>(element, message) in http://mysite/Scripts/jquery.validate.js:
    this.settings.errorPlacement(label, $(element));
called from line 581, column 6 in <anonymous function: defaultShowErrors>() in http://mysite/Scripts/jquery.validate.js:
    this.showLabel(this.successList[i]);
called from line 377, column 5 in <anonymous function: showErrors>(errors) in http://mysite/Scripts/jquery.validate.js:
    this.defaultShowErrors();
called from line 326, column 4 in <anonymous function: form>() in http://mysite/Scripts/jquery.validate.js:
    this.showErrors();
called from line 72, column 5 in <anonymous function: validate>(event) in http://mysite/Scripts/jquery.validate.js:
    if (validator.form())
called via Function.prototype.apply() from line 3073, column 5 in <anonymous function: dispatch>(event) in http://mysite/Scripts/jquery-1.9.1.js:
    ret = ( (jQuery.event.special[ handleObj.origType ] || {}).handle || handleObj.handler )
called via Function.prototype.apply() from line 2749, column 4 in <anonymous function: elemData.handle>(e) in http://mysite/Scripts/jquery-1.9.1.js:
    return typeof jQuery !== core_strundefined && (!e || jQuery.event.triggered !== e.type) ?

我的表格:

@using(Ajax.BeginForm("action",
                       "controller",
                       null,
                       new AjaxOptions
                           {
                               HttpMethod = "Post",
                               InsertionMode = InsertionMode.InsertAfter,
                               OnComplete = "completeRequest",
                               UpdateTargetId = "update-div"
                           }, new { @id = "my-ajax-form" }))
{...}

我的 java 脚本文件和completeRequest方法

$(document).ready(function () {
    initializeSubmit();
});    

function initializeSubmit(){
    $('#myinput').change(function() {
        alert(); // alert works and after that I get that big error in browsers console
        $('#my-ajax-form').submit();
    });
}

function completeRequest(data) {
        var result = $.parseJSON(data.responseText);

        // do stuff
    }

Google chrome浏览器的控制台输出:

ncaught SyntaxError: Unexpected token u       jquery-1.9.1.js:541
jQuery.extend.parseJSON                       jquery-1.9.1.js:541
onError                                       jquery.validate.unobtrusive.js:41
proxy                                         jquery-1.9.1.js:818
$.extend.showLabel                            jquery.validate.js:698
$.extend.defaultShowErrors                    jquery.validate.js:655
$.extend.showErrors                           jquery.validate.js:410
$.extend.form                                 jquery.validate.js:358
(anonymous function)                          jquery.validate.js:83
jQuery.event.dispatch                         jquery-1.9.1.js:3074
elemData.handle                               jquery-1.9.1.js:2750
jQuery.event.trigger                          jquery-1.9.1.js:2986
(anonymous function)                          jquery-1.9.1.js:3677
jQuery.extend.each                            jquery-1.9.1.js:648
jQuery.fn.jQuery.each                         jquery-1.9.1.js:270
jQuery.fn.extend.trigger                      jquery-1.9.1.js:3676
jQuery.fn.(anonymous function)                jquery-1.9.1.js:7403
(anonymous function)                          *******my-javascript-file.js:171******
jQuery.event.dispatch                         jquery-1.9.1.js:3074
elemData.handle                               jquery-1.9.1.js:2750

my-javascript-file.js:171实际上是一行,其中调用了提交:

function initializeSubmit(){
        $('#myinput').change(function() {
            alert(); // alert works and after that I get that big error in browsers console
171:       $('#my-ajax-form').submit();
        });
    }

这是实际的 jquery 源代码,它会引发异常:

if ( !(eventHandle = elemData.handle) ) {
            eventHandle = elemData.handle = function( e ) {
                // Discard the second event of a jQuery.event.trigger() and
                // when an event is called after a page has unloaded
                return typeof jQuery !== core_strundefined && (!e || jQuery.event.triggered !== e.type) ?
        2750: jQuery.event.dispatch.apply( eventHandle.elem, arguments ) :
                    undefined;
            };
            // Add elem as a property of the handle fn to prevent a memory leak with IE non-native events
            eventHandle.elem = elem;
        }

jQuery第541

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

这是 jQuery 错误吗?或者可能是什么问题?我已经被困在这里大约几个小时了。

PS在提交之前会抛出错误。表格尚未发布。

这是 chrome 调试器中的错误状态。 在此处输入图像描述

PPS我找到了奇怪的解决方案:我@Html.ValidationMessageFor()为所有模型属性添加了助手,并且我已经成功发布了表单,得到了服务器端并在控制器中达到了我的断点。仍然无法理解这个问题在哪里。

4

0 回答 0