2

I've been trying this out for about a day already and looked through multiple questions and answers and still unable to solve this.

Regardless, my name value will always be null or end up with a bad request error 400. Really would appreciate if someone could tell me what I'm doing wrong here.

Javascript:

var myPerson = {};
    myPerson.name = "Brandon";

    function createObject() {
        $.ajax({
            contentType: "application/json;charset=utf-8",
            dataType: 'json',
            type: 'POST',
            url: '/Person/CreatePerson',
            data: JSON.stringify(myPerson),
            success: function (response) {
                alert(JSON.stringify(myPerson);
            }, error: function (e) {
                alert(e.responseText);
            }

        })
    }

Controller:

    [HttpPost]
    public ActionResult CreatePerson(string name)
    {
        Person person = new Person();

        person.Name = name;

        _context.Person.Add(person);
        _context.SaveChanges();
        return Json(person);
    }

alert results

15
  • 1
    remove contentType: 'application/json', there's no real need for it. Just send the data in the normal form-encoded format Commented May 23, 2018 at 8:55
  • Still returns blank, response returns [object Object] Commented May 23, 2018 at 9:02
  • So are you now saying the name property isn't posted or the response is wrong? Commented May 23, 2018 at 9:05
  • 1
    If you alert an object is will say [object Object] because it's an object... Commented May 23, 2018 at 9:05
  • 1
    alert(JSON.stringify(response)); will give you a better idea of the response data. response is a JSON object, not plain text, so you can't just print it directly Commented May 23, 2018 at 9:06

1 Answer 1

2

I have been struggling too with this because I thought it was very weird. In ASP.NET Core 2.0 you have to add a [FromBody] before your parameters in your action. Here is why.

public ActionResult CreatePerson([FromBody] string name)

For the Person response you might need this

return Json(new {person = person});

Read it in your ajax like this:

var person = response.person

UPDATE

Since you want to pass the person object do the following

var myPerson = {
    "Name" = "personName"
};

Pass it like this

data: JSON.stringify(myPerson)

And get it to the controller like this:

public ActionResult CreatePerson([FromBody] Person myPerson)
Sign up to request clarification or add additional context in comments.

3 Comments

var person = JSON.parse(response);...no because dataType: "json" in the ajax options ensures that jQuery has already parsed it into an object. OP's issue (as per comments) is in displaying that, for which the opposite - stringify - is required
"You have to also add the property dataType "...it's already there in the original code. And why are you proposing making the response object more complex?
Added this and encountered this error :Failed to load resource: the server responded with a status of 415 ()

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.