The Asynchronous interaction between the client and the server is implemented using the Ajax method provided by jquery. The following describes how to use Ajax functions in the extracted jquery API:
There is a data parameter that allows you to transmit data to the server. jquery will serialize the parameter data based on the post or get protocol.
If the submitted data uses complex JSON data, for example:
{Userid: 1, userinfo: {name: "Lily", age: "21 "}}
The server cannot normally receive complete parameters because jquery serializes data and uses key-value pairs to assemble the data. Parameters are assembled into userid = 1 & userinfo = object; the objects pointed to by userinfo are serialized into strings "object ".
How can I submit a complex object to the background action parameter? Here we provide a JSON. js file whose serialized data is in JSON format (see the source code). The $. tojson (PARAM) function is called, and you can also write it yourself.
var json = { Name: "Sherry", Age: "21" };
$("#btnJson_post").click(function () {
$.ajax({
type: "POST",
url: "/Home/AcceptJson",
dataType: 'json',
data: $.toJSON(json),
contentType: 'application/json; charset=utf-8',
success: function (msg) { alert(msg.Name); }
});
});
Server:
Person. CS
[Serializable]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Homecontroller
[HttpPost]
public JsonResult AcceptJson(Person person)
{
return Json(new Person { Name=person.Name,Age=person.Age });
}
However, the person parameter on the server cannot receive data. Because deserialization of data cannot be performed in JSON format, you need to add references to Microsoft in MVC 2.0. web. MVC. DLL, and in the global. asax. add the following code at the end of the CS file in application_start:
Valueproviderfactories. Factories. Add (New jsonvalueproviderfactory ());
In this way, MVC can deserialize data in JSON format and process the data and return results. Source code download
Also can refer to: http://www.mikel.cn/%e5%bc%80%e5%8f%91%e7%ac%94%e8% AE %b0/%e8%bd%ac%e8%bd%bdasp-net-mvc%e4%b8%ad%e7%9a%84json-binding%e5%92%8cvalidate.html