In the next chapter,
HttpPost Request
1. Post request, single parameter
Front
var url = ' Api/enterorexit/getdata2 ';
var para = {};
para["Phone"] = "phone13880825221";
para["UID"] = "uid287572292";
$.post (URL, para, function () {}, "Application/json");
Back end
[HttpPost]
Public Ihttpactionresult GetData2 (string phone)
{
string result = "interface has gone through";
return ok<string> (Result);
}
This, this is a fucking monster, why, this is similar to get, I can not receive,
Oh, do you remember the knowledge of the previous article? The data from the GET request in the HTTP protocol is appended to the URL (that is, the data is placed in the HTTP protocol header), and the post request is placed in the package body of the HTTP protocol package.
In the GET request of the previous article, when the entity is passed, [fromuri]-because of the GET request, the parameter data is placed in the HTTP protocol header, and the post is placed in the package body of the HTTP protocol package, for example, inferred that there must be a [frombody] and so on, the truth is better than bragging, try again,
Front
var url = ' Api/enterorexit/getdata2 ';
var para = {};
para["Phone"] = "phone13880825221";
para["UID"] = "uid287572292";
$.post (URL, para, function () {}, "Application/json");
Back end
[HttpPost]
Public Ihttpactionresult GetData2 ([frombody]string phone)
{
string result = "interface has gone through";
return ok<string> (Result);
}
Yeah, breakpoint into the Action,but,phone=null, why, consult the relevant great God wrote blog, through the URL parameter mechanism is a key value pair, that is, a key equals a value, and here the frombody and we generally through the URL to take the parameters of the mechanism is different , its mechanism is =value, a single parameter, the great God said to change,
Front
var url = ' Api/enterorexit/getdata2 ';
var para = {};
Para[""] = "phone13880825221";
$.post (URL, para, function () {}, "Application/json");
The back end is the same, OK, the phone does get the value.
************************************************************************************
2. Post request, multiple parameters
Since the mechanism by which the post takes parameters from the request body differs from the mechanism of get parameters from the URL, the post is =value,
Request Multi-parameter-Convert to request entity-Convert to request single parameter problem,
Multi-parameter wrapper into entity class, entity class wrapped in JSON string format
Front
var url = ' Api/enterorexit/getdata2 ';
var para = {};
Para[""] = "phone13880825221";
para["UID"] = "uid287572292";
var str = json.stringify (para);
var data= {"", str}
$.post (URL, data, function () {}, "Application/json");
Back end
[HttpPost]
Public Ihttpactionresult GetData2 ([frombody]string requeststr)
{
var model= newtonsoft.json.jsonconvert.deserializeobject<tb_requestmodel> (REQUESTSTR);
string result = "interface has gone through";
return ok<string> (Result);
}
Requeststr gets the JSON format string and deserializes it into an entity.
If we pass a POST request with multiple parameters each time, we need to modify the entity each time, for the parameters that are not often modified, of course, the above method is enough, each time the parameter is changing, the great God provides a solution,
Front
var url = ' Api/enterorexit/getdata2 ';
var para = {};
Para[""] = "phone13880825221";
para["UID"] = "uid287572292";
var data= json.stringify (para);
$.post (URL, data, function () {}, "Application/json");
Back end
[HttpPost] public object GetData2 (Dynamic obj) { var uid= convert.tostring (obj). UID);
string result = "interface has gone through";
return ok<string> (Result);
}
One thing to note is that in the AJAX request there is a need to add the parameter type JSON, namely ContentType: ' Application/json ', this property , which is the AJAX request, requesting Content-type, I'll talk about response's Content-type later on.
--------------------------------------------------------------------------------------------------------------- ------
3. Post request, Entity parameters
When using the entity as the parameter, the front-end passes the normal JSON directly, the background directly uses the corresponding type to receive can, does not have the frombody. But one thing to note here is that you cannot specify ContentType as Appplication/json, otherwise, parameters cannot be passed to the background. The default Contenttype:application/x-www-form-urlencoded,form form data in the HTTPPOST,REQUEST request is encoded as key/ The value format is sent to the server (the format of the form's default submission data)
Front:
var url = ' Api/enterorexit/getdata2 ';
var para = {};
Para[""] = "phone13880825221";
para["UID"] = "uid287572292";
$.post (URL, data, function () {});
Back end:
[HttpPost]
Public Ihttpactionresult Test (Requestmodel model)
{
String msg = "";
String code = "";
string result = "interface has gone through";
return ok<string> (Result);
}
Model is not empty, and each property has a value
PS, note the above front-end code, $.post (URL, data, function () {}); no content-type for request is specified,
The
POST request defaults to sending the Key/value form of the data inside the form to the service, and our server only needs the object with the corresponding Key/value property value to receive it. Using Application/json, however, means that the front-end data is passed to the backend with serialized JSON, and the back end is going to turn it into a solid object, and a deserialization process is required. According to this logic, if we specify ContentType as Application/json, then passing the serialized object should also be possible.
The following is also possible,
Frontend:
var url = ' api/enterorexit/getdata2 ';
var para = {};
para[""] = "phone13880825221";
para["UID"] = "uid287572292";
$.post (URL, data, function () {}, "Application/json");
Back end:
[HttpPost]
Public ihttpactionresult test (Requestmodel model)
{
string msg = "";
String code = "";
String result = "interface Pass";
return ok<string> (result);
}
summarize the knowledge point: In an HTTP request, if you specify ContentType as Application/json, The serialized object must be passed, and if the default parameter type of the POST request is used, the front-end passes directly to the JSON-type object.
Post Request API, there are several special, array, collection, but because I do not use in the work, so there is no say, but the use of the method is the above basic parameters evolved, to make clear the principle and the way, the detailed use of the person can cope with the requirements of a variety of parameter requests.
(iii) pits in the ASP. NET Web API-"parameters in an HTTP POST request"