0

I've read every reference to "Invalid JSON primitive" with no success, so I have no choice but to post my own. I have reduced my code down to the bare minimum and am still getting the error. Here's the "bare minimum":

var dataObject = {
    'FirstName': $('#FirstName').val(),
    'LastName': $('#LastName').val()
};

$.ajax({
    type: 'POST',
    url: '@Url.Action("Submit", "Home")',
    contentType: 'application/json;',
    dataType: 'json',
    data: dataObject,
    success: function (result) {
        debugger;
        alert('Success');
    },
    error: function (response) {
        debugger;
        alert('Error');
    }
});

I have also tried replacing the dataObject with:

var dataObject = {
    'FirstName': 'ES',
    'LastName': 'Dictor'
};

Giving no change to my results.

No matter what I do I get (Chrome console):

response:
Object {readyState: 4, responseText: "<!DOCTYPE html>
↵<html>
↵    <head>
↵        <titl…nStep step, Boolean& completedSynchronously)
↵-->", status: 500, statusText: "Internal Server Error"}

and if I look at response.responseText I see:

<!DOCTYPE html>
<html>
    <head>
        <title>Invalid JSON primitive: FirstName.</title>

I believe I've tried everything at this point, so it's probably something simple that I'm just missing. Hoping that somebody can see what I can't.

7
  • @DavidTansey The HTML is just giving me the error .. if it wasn't failing I'd expect JSON. Commented Oct 6, 2015 at 16:07
  • We would need to see what your post does on the back end. It could be accessing a database incorrectly or referencing pages that don't exist something like that which could result in a internal server error. Commented Oct 6, 2015 at 16:08
  • @Demodave my assumption was that the "invalid json primitive" error meant that it wasn't getting that far. Am I incorrect? Commented Oct 6, 2015 at 16:10
  • @ESDictor -- sorry, I was not looking closely enough. Have you tried using JSON.stringify() on the data? Something like data: JSON.stringify(dataObject) ? Commented Oct 6, 2015 at 16:10
  • @DavidTansey earlier you mentioned the error being returned in HTML, not JSON. Based on that I commented out the "contentType" line and am now getting some success. Not sure if I'm 100% there yet, but I'm closer. Commented Oct 6, 2015 at 16:35

1 Answer 1

1

You posting back an object, not json, so you need to remove the contentType: 'application/json;', ajax option so that it uses the default (which is 'application/x-www-form-urlencoded; charset=UTF-8') Alternatively you need to use JSON.stringify() to convert the object to a JSON string.

Side note: It is not necessary to quote the property names. It can be

var dataObject = {
    FirstName: $('#FirstName').val(),
    ....
Sign up to request clarification or add additional context in comments.

1 Comment

You are correct that it's not necessary to quote the property names, but it actually has no effect to do so. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.