NetCore pass the Senate changes

A: $, ajax in, Content-Type, do not write, default
 application/x-www-form-urlencoded

 

 
  

 

Common is this kind of
Method rear end, there is no need to add any characteristic parameters and the plurality of transmission entities, can get, get the possible default .NetCore form.
 
Two: Content-Type Once replaced application / json ;, backend must add [FormBody] characteristics in order to get the parameters,
. 1    ///  <Summary> 
2          /// incoming request native embodiment of an Ajax + Other physical parameters through data serialization, the data in the RequestPayload
 . 3          ///  </ Summary> 
. 4          ///  <param name = "jData"> </ param> 
. 5          [HttpPost]
 . 6          [the Route ( " / GET / HH " )]
 . 7          public IActionResult fdasfsae ([FromBody] KJ L, [FromBody] int YY, [FromBody] String BB)
 . 8          {
 . 9              // NetCore, the transport format contentType: "file application / JSON; charset = UTF-. 8"
 10              // rear must be added [FromBody] to get the value, and can be bound into a single entity, l binding is successful , the general parameters and bb yy get value
. 11              #region commented code
 12 is              // $ .ajax ({
 13 is              //      type: "POST",
 14              //      URL: "/ GET / HH",
 15              //     Data: the JSON.stringify ({A:. 1, B: 2, c: 4, "yy ": 99, "bb": " match"})   // if not serialized, not obtain a rear end, of the sequence, also you need to add FormBody, to get value, FromForm can not get the value
 16              //      contentType: "file application / JSON; charset = UTF-. 8"
 . 17              // });
 18 is              // rear end can not get the value added [FromForm] default value, added [FromBody] direct is null 
. 19              #endregion 
20 is  21 is return Json ( " the OK " );
22                      }

 The figure is an entity parameters and other common parameters, general parameter takes a value less than

[HttpPost] 
[the Route ( " / GET / kJ " )]
 public IActionResult IO ([FromBody] kmmm KJ, [FromBody] int YY, [FromBody] String BB) 
{ 
           /// joined [FromBody], can not receive parameter 
           // kmmm can be bound into a solid model, and the general parameters bb not get the value yy 
}

 

Solution:

Writes an object, this is it ah, NetCore bound to an object on the line i. So transmission application / json is not convenient, you may need to rewrite the implementation model binding, you can get by Jobject

 

 

  ///  <Summary> 
        /// more parameters of the native Ajax request method of acquiring, through the data serialization data in RequestPayload, the physical parameters of the whole into a common reference
         ///  </ Summary> 
        ///  < name = param "jData"> </ param> 
        ///  <Returns> </ Returns> 
        [HttpPost] 
        [the Route ( " / GET / kJ " )]
         public IActionResult IO ([FromBody] Newtonsoft.Json.Linq.JObject jData ) 
        { 
            // function EE () {
             //       var = {A Model:. 1, B: 2, C:. 4,};
             //       var ARR = [{ "A":. 1, "B":. 1, "C ": 2," c ":
             3}];//      / * Transmission json array 
             //     the JSON.stringify JSON = var (ARR);
             //      var KHG =
             //      {
             //             K: ARR,    // direct the array can not, with the rear end of the received Jobject, serialization error
             //             // K: Model,   // write directly json objects is possible
             @             YY: 34 is,
             @             BB: "okay",
             //        }; 
//         $ .ajax ({
             //             type: "POST",
             //             URL: "/ GET / kJ ",
             //             the Data: JSON.stringify (KHG),
            //            contentType: "application/json;charset=utf-8"
            //        });
            //  }
            //dynamic json = jData;
            //Newtonsoft.Json.Linq.JObject jUser = json.k;
            //KJ kmmm = jUser.ToObject<KJ>();
            //int yy = json.yy;
            //string bb = json.bb;
return Json("OK");
        }
​

Three: $ ajax and $ .post (), it is the same, the default Content-Type is application / x-www-form-urlencoded.

 

Guess you like

Origin www.cnblogs.com/Qintai/p/11829523.html