Passing entity parameters under WebAPI Get||Post requests and solving API cross-domain problems

Get

front desk code

 

   <script>



        $(function () {
            $.ajax({
                type: "get",
                dataType: "json",
                contentType: 'application/json',
                url: "http://localhost:19930/omy360api/HomeAPI/GetToken",
                data: { "UserName": "wly666", "PassWord": "123456" },
                success: function (data, status) {
                    console.log(data);
                },
                error: function (e) {

                },
                complete: function () {

                }

            });
        });
    </script>

 

  /// <summary>
        /// 生成Token的令牌
        /// </summary>
        /// <param name="us">userinfo</param>
        /// <returns>Objetc</returns>
        [HttpGet]
        public object GetToken([FromUri]UserInfo us)

The above picture (2) is the background code, the key lies in

The [FromUri] feature 
Get originally passed the parameters named after the method of the Get type concatenated by the URL string. The
naming method of "Get + method name" should be used as much as possible, and the [HttpGet feature] should be added before the method
. The matching of formal parameters follows the routing rules.
In addition to passing entity parameters in this way, GET can also be passed as a string format JSON background conversion.

Post
  $.ajax({
               type: "Post",
               url: "/omy360api/HomeAPI/PostToken",
               data: { "Username": "XXXXX","Password":"123456"}
            })
 [HttpPost]
        public object PostToken([FromBody]UserInfo us)

Basically, this is enough. Just do what you need to do next. It is enough to pass the entity parameters like this~ 

 

 

You can't reach a thousand miles without accumulating a few steps, and you can't make a river without considering small streams~



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325131472&siteId=291194637