1

> I .net core 2.2 This is the object Object:

[Serializable]
public class oob
{
   public int i { get; set; }
   public string j { get; set; }
}

this is the action in "Home" Controller named Gett that takes oob as input from ajax

 [HttpGet]
public IActionResult Gett(oob ww)
{
    return Ok(ww);
}

Ajax

    {
        $.ajax({
            type: "Get",
            url: "Home/gett",
            data: { ww: JSON.stringify({i:55,j:"weqe"})},
            dataType: "json",
            contentType:"json",
            success: function (f) {
                console.log(f);
            },
            error: function (f) {
                console.log(f);
            }
        });
    });

When request is made ,at the Gett(oob ww) i get an object with value of i=0 and j=null

3
  • You mean: return Ok(ww) instead of OK(i). right ? Commented Feb 10, 2019 at 8:15
  • Sorry yes @ Abdullah Dibas Commented Feb 10, 2019 at 8:17
  • Try ContentType 'application/json' Commented Feb 10, 2019 at 8:44

1 Answer 1

1

Ideally you should not pass object to a GET request, for posting object, you should use POST.

If you still want, you need to change your GET method like following using FromQuery.

[HttpGet]
public IActionResult Gett([FromQuery] oob ww)
{
    return Ok(ww);
}

And change your AJAX call like following.

$.ajax({
            type: "Get",
            url: "Home/gett",
            data: {i:55,j:"weqe"},
            dataType: "json",
            contentType:"json",
            success: function (f) {
                console.log(f);
            },
            error: function (f) {
                console.log(f);
            }
        });

Note: To pass the object you don't need JSON.stringify if you are using FromQuery for your API

Sign up to request clarification or add additional context in comments.

4 Comments

can you change data:{ ww: JSON.stringify({i:55,j:"weqe"})} to data:JSON.stringify({i:55,j:"weqe"})
.net core 2.2 MVC controller
just change data: {i:55,j:"weqe"}, I tested locally and its working
and use [FromQuery]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.