MVC default Request mode is Post.
Action
Public Jsonresult Getpersoninfo ()
{
var person = new
{
Name = "Zhang San",
Age = 22,
Sex = "Male"
};
return Json(person);
}
Or
Public Jsonresult Getpersoninfo ()
{
return Json (new{Name = "Zhang san", age = 22,sex = "Male"});
}
View
$.ajax ({
URL: "/friendlink/getpersoninfo",
Type: "POST",
DataType: "json",
Data: {},
Success:function (data) {
$ ("#friendContent"). HTML (data. Name);
}
})
There is no problem with the POST request, the GET method request error:
Workaround
The JSON method has a refactoring:
protected internal Jsonresult Json(object data);
protected internal Jsonresult Json(object data, jsonrequestbehavior behavior);
We just need to use the second type, plus a JSON request behavior is OK for Get mode
Public Jsonresult Getpersoninfo ()
{
var person = new
{
Name = "Zhang San",
Age = 22,
Sex = "Male"
};
return Json(person,jsonrequestbehavior. Allowget);
}
In this way we can request it in the front-end using get:
$.getjson ("/friendlink/getpersoninfo", NULL, function (data) {
$ ("#friendContent"). HTML (data. Name);
})
How to use Jsonresult under MVC (jsonrequestbehavior.allowget)