This topic is not duplicate with others my problem is in get method
i have this data:
var things = [
{ "name": "n1" },
{ "name": "n2" }
];
Ajax call:
$.ajax({
url: /controller/GetList,
data: JSON.stringify(things),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
Server side:
public JsonResult GetList(
List<Thing> things) => Json("OK");
public class Thing
{
public string name { get; set; }
}
Now when i call ajax with method: “POST” and get data with [FromBody] in serverside everything is ok, but if i want to call ajax with GET method things in server side is null or empty, WHY?
I try this:
data: JSON.stringify({ things: things })
traditional: true
But it does not work
My project is in Asp.net Core 3.1
3
Answers
You should avoid sending data (in the request body) for
GET
requests, because it could have complications. Many services, don’t expectGET
requests to have a body and, thus they just ignore it.If it is not a lot of data you could try sending it in the query string, but I suggest just using
POST
Simply specify the method to GET:
If you indeed want to make
GET
HTTP request and pass data on JavaScript client, you can try to pass them through querystring, like below.Action
GetList
Test Result