简体   繁体   中英

Why ASP MVC model binder only accept JSON in POST?

Whenever I send 'GET' with JSON.stringify() using AJAX, model value is always accept null; Why it can only bind 'POST' ? If it is possible, can I use 'GET' and still bind data to model?

Edit: adding Code Example

JS:

$.ajax({
        var modelsend = {
            itemname: 'shoe',
            itemcolor: 'red',
            itemsize: '31',
            itemvariety: 'SR-31',
        }
        type: "POST",
        url: "@Url.Action("ShowData", "Controller")",
        data: JSON.stringify(modelsend),
        dataType: "json",
        contentType: "application/json",
        success: function (data) {
            //do something with data
        },
        error: function (jqXHR, textStatus, errorThrown) {
            //show error
        }
    });

Model:

public class shoemodel
{
    public string itemname { get; set; }
    public string itemcolor { get; set; }
    public string itemsize { get; set; }
    public string itemvariety { get; set; }
}

Controller:

public ActionResult ShowData(shoemodel get)
{
    List<DataGrid> fetch = func.getdata(get);
    return Json(fetch);
}

Perhaps you are forgetting that GET is used for viewing something, without changing it, while POST is used for changing something. And Get can be used to change something only when you use Querystring. Post on the other hand sends form data directly.

HTTP 'GET' method doesn't support a body in the request. The way to send parameters via 'GET' is using the application/x-www-form-urlencoded format appending them in the URL like this.

http://example.com/?key1=value1&key2=value2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM