0

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?

8 Answers 8

3

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"
};
Sign up to request clarification or add additional context in comments.

2 Comments

Without "Json.stringify" returned: Error 500 =[
I discovered the problem, I had two actions with the same name so the error was happening. Anyway appreciate the help! :)
1

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

data: { name: "Renato Leite" },

Comments

1

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?

Comments

1

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

[HttpPost]

Comments

1

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

Comments

1

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"});

Comments

0

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

Comments

0

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.

Comments

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.