P.Brian.Mackey
P.Brian.Mackey

Reputation: 44295

SyntaxError: JSON.parse: unexpected character in MVC

When I submit my form with jQuery:

$('#saveButton').click(function () {                
                if (true) {
                    $('#submitForm').submit();
                } 
            });

I get an error in the file jquery....min.js:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I'm using asp.net MVC to generate the form:

@using (@Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { id = "submitForm" }))

Submit Buttons. I am using the top one. The button one is not clicked when this happens:

<input type="button" id="saveButton" class="bigButton" value="Save" />
<input type="submit" id="submitRequestButton" class="bigButton" value="Submit" />

I look in the network tab on FF and I don't see a POST. How can I track down or narrow down what is causing this problem?

Update

The code in the console window is all I get and its pointing me to the minified jQuery lib:

/*! jQuery v2.1.0 | (c) 2005, 2014 jQuery Foundation, Inc. | jquery.org/license */ !function(a,b){"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){var c=[],d=c.slice,e=c.concat,f=c.push,g=c.indexOf,h={},i=h.toString,j=h.hasOwnProperty,k="".trim,l={},m=a.document,n="2.1.0",o=function(a,b){return new o.fn.init(a,b)},p=/^-ms-/,q=/-([\da-z])/gi,r=function(a,b){return b.toUpperCase()};o.fn=o.prototype={jquery:n,constructor:o,selector:"",length:0,toArray:function(){return

...and this goes on for about 1000 more lines or so.

Upvotes: 0

Views: 2088

Answers (1)

P.Brian.Mackey
P.Brian.Mackey

Reputation: 44295

I fixed the problem by removing the MVC validation scripts and my own custom validation scripts from the code:

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/conditional-validation.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/customItemRequestValidation.js")" type="text/javascript"></script>

I wrote new custom validation that is not based on MVC's validation. I have no need for MVC's built in validation. I don't know why one of these files is causing a JSON parse error.

The old custom validation scripts that I removed did stuff like:

$.validator.addMethod('requiredif',

and

$.validator.unobtrusive.adapters.add(
    'requiredif',

My guess is one of my old custom validation scripts was messed up or my new validation scripts conflict with MVC's validation scripts somehow.

Upvotes: 2

Related Questions