ASP. NET Webapi+jquery Ajax GET request collation
First, summary
The 1.WEBAPI supports post-commit processing by default, and the returned result is a JSON object, and the foreground does not require a manual deserialization process.
2.WebApi receive post submission parameters need to be specified ([Frombody] string name)
3.WebApi If you receive only one underlying type parameter, you cannot specify the name of the key
4.WebApi POST request action can only receive one parameter, meaning [Frombody] can only be specified once in an action
5.WEBAPI POST request processing multiple parameters you can receive parameters using the Class object Method Example: Student
6. If you do not want to define a class when you receive the post parameter, you can use the Newtonsoft.Json.Linq jobject Json object container to receive the parameter
7. (deprecated) This receive parameter can use the Jobject that is used by the dynamic nature, but the commit parameter needs to specify the string type, ContentType: ' Application/json ', similar to the specified way in webserver
8. in the POST request processing of WEBAPI, the action name in the background cannot be named with the "GetXXX" method
Second, the Verification code
1. Single parameter passing
The post get request parameter needs to specify the source of the parameter [Frombody],
When Post is submitted, the name of the action cannot begin with the ' Get ' name,
If you receive only one underlying type parameter, you cannot specify the name of the key
Background:
/// <summary>///The post GET request parameter needs to specify the source of the parameter [Frombody]/// </summary>/// <param name= "name" ></param>/// <returns></returns> Public stringShowName ([Frombody]stringname) { return$"name you passed in: ' {name} '";} Publicdictionary<string,string> findlist ([frombody]BOOLisshow) {Dictionary<string,string> dict =Newdictionary<string,string>(); if(isshow) {Dict. ADD ("name1","Zhang San"); Dict. ADD ("name2","John Doe"); } returndict;}
Javascript:
$.post ('/api/postuser/showname ', { ': ' Sam Fung 'function (data) { Console.info (data); alert (data);}); $.post ('/api/postuser/findlist ', { truefunction ( Data) { console.info (data); alert (data);});
Two, multiple parameter passing
1. Specify the class type Student
Background:
Public class student{ publicstringgetset;} Public int Get Set ; }}
/// <summary> /// Post get parameter can receive object type, receive multiple parameters /// </summary> /// <param name= "Stu" ></param> /// <returns></returns> Public string ShowName2 ([frombody] Student stu) { return $"' {stu. The Age of Name} ' is: {stu. AGE}";}
Javascript:
$.post ('/api/postuser/showname2 ', { ' Zhang Sanfeng ', 'function ' (data) { console.info (data); alert (data);});
2. Using Jobject
Background:
// /<summary> // /When receiving the post parameter, if you do not want to define a class, you can use Newtonsoft.Json.Linq's Jobject Json object container to receive parameters // /</summary> // /<param name= "obj" ></param> // /<returns></returns> Public Object ShowName3 ([frombody] Jobject obj) { returnnewtrue };}
Javascript:
// If you use Jobject, you can get success with object submission or using string submission $.post ('/api/postuser/showname3 ', { ' Zhang Sanfeng ', function ( data) {console.info (data); alert (data);});
3. Using dynamic (not recommended)
Background:
// /<summary> // /When receiving the post parameter, if the foreground incoming parameter is a string, you can use the dynamic type to receive it, and you do not need to specify [Frombody] // /Here the true type of obj: FullName = "Newtonsoft.Json.Linq.JObject" // /</summary> // /<param name= "obj" ></param> // /<returns></returns> Public Object ShowName3 (Dynamic obj) { returnnewtrue };}
Javascript:
// you need to specify the parameter type: contentType: ' Application/json ', similar to the specified method in webserver $.ajax ({ '/api/postuser/showname3 ', type:' post ', ' Application/json ' ) , ' Zhang Sanfeng ', age:19 }), function (data) { console.info (data); alert (data);} });
For more information:
ASP. NET WEBAPI Get request collation (i)
ASP. NET WebAPI get commit, post commit processing
"Get" started in ASP. WEBAPI Action naming
ASP. WebApi POST Request collation (i)