每当我使用AJAX发送'GET'
JSON.stringify()
时,模型值始终为null;
为什么它只能绑定'POST'
?
如果可能,我可以使用'GET'
并仍将数据绑定到模型吗?
编辑:添加代码示例
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
}
});
型号:
public class shoemodel
{
public string itemname { get; set; }
public string itemcolor { get; set; }
public string itemsize { get; set; }
public string itemvariety { get; set; }
}
控制器:
public ActionResult ShowData(shoemodel get)
{
List<DataGrid> fetch = func.getdata(get);
return Json(fetch);
}
答案 0 :(得分:1)
也许您忘记了GET用于查看内容而不更改内容,而POST则用于更改内容。只有在使用Querystring时,Get才能用于更改内容。另一方面,帖子直接发送表格数据。
答案 1 :(得分:0)
HTTP 'GET'
方法不支持请求中的正文。通过'GET'
发送参数的方式是使用application/x-www-form-urlencoded
格式将其附加到网址中。