Renato Leite
Renato Leite

Reputation: 791

ASP.NET MVC4 C# Problems to post JSON data

I'm having problems to post json data on ASP.NET MVC 4.

When I call the ajax function I receive the following error: 302 Found.

jQuery Code:

var dataJson = [];

dataJson.push({
    name: "Renato Leite"
});

$.ajax({
    url: "/Controller/MyAction",
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify(dataJson),
    contentType: 'application/json; charset=utf-8',
    async : false,
    success: function (data) {
        var message = data.Message;
    }
});

C# Code:

public ActionResult MyAction(string name)
{
    return Json(new { Message = "Sucess" }, JsonRequestBehavior.AllowGet);
}

The return of request:

Status Code: 302 Found

How to solve this?

Upvotes: 0

Views: 1387

Answers (8)

van
van

Reputation: 1

Try changing your Controller Action's parameter into an Enumerable of strings, such as string[] name or IEnumerable name. The square brackets are for arrays.

Upvotes: 0

Renato Leite
Renato Leite

Reputation: 791

I discovered the problem, I had two actions with the same name so the error was happening. Anyway appreciate the help! :)

Upvotes: 0

pnewhook
pnewhook

Reputation: 4068

As @nboisvert pointed out you're posting so your Action needs an [HttpPost] attribute. And as @Satpal pointed out your JSON payload is actually an array of objects being posted to a method that only takes a string argument.

var dataJson = [];

dataJson.push({
    name: "Renato Leite"
});
JSON.stringify(dataJson); // "[{"name":"Renato"}]" notice the square brackets

You would get away with posting simply

data: JSON.stringify({name: "Renato Leite"});

Upvotes: 1

ramiramilu
ramiramilu

Reputation: 17182

Check this solution -

Controller -

public ActionResult SubmitTag(string test)
{
    return Json(new { Message = "Sucess" }, JsonRequestBehavior.AllowGet);
}

View -

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>
    $(function () {
        $("#ClickMe").click(function () {

            var tag = {
                "test": "SampleTag"
            };

            $.ajax({
                url: "@Url.Action("SubmitTag")",
                type: "POST",
                data: JSON.stringify(tag),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert(data.Message);
                },
                error: function (response) {
                    alert(response.responseText + "e");
                }
            });
        });
    });
</script>

<input type="button" id="ClickMe" value="ClickMe" />

Output -

enter image description here

Upvotes: 1

Nicolas Boisvert
Nicolas Boisvert

Reputation: 1171

You should add the following attribute to your action method, since your ajax call uses 'POST' method

[HttpPost]

Upvotes: 1

Joel
Joel

Reputation: 2257

Strange, I put your exact code into my project and I'm getting the correct data returned.

Maybe try changing your result to a JsonResult instead of an ActionResult?

Upvotes: 1

Matt Bodily
Matt Bodily

Reputation: 6423

you don't need to stringify a single string. try replacing the data line with this

data: { name: "Renato Leite" },

Upvotes: 1

Satpal
Satpal

Reputation: 133403

Your action accept only a string parameter. Currently you are passing an array of strings. Thus you are getting error

Just use

var dataJson = {
    name: "Renato Leite"
};

Upvotes: 3

Related Questions