Basic operations of js on WebApi requests and basic operations of jswebapi

Source: Internet
Author: User

Basic operations of js on WebApi requests and basic operations of jswebapi

WebAPI provides four external interfaces: get, post, delete, and put. Now, I will briefly discuss the methods and general functions of js request to webApi;

Get: In webApi, The get method is usually used to obtain data, and the frontend request method is usually $. get (url, function (d) {}), The passed parameters are included in the url; for example, url + "? Id = 1 "; there are multiple values that can also be passed, not to mention;

Post: In general, post is used to transmit data to the server, through jq's $. the post () method is sufficient to request data. In webApi, Public bool Post ([FromBody] Model model) {}is added. In a controller, only one [FromBody] is allowed. that is to say, only one model can be passed in the post request, but there is no limit on the content in the model. The jq request method can be $. post (url, data, function () {}); there is $. ajax ({type: "post"}); for details about pos requests, refer to another essay I wrote. Click here;

Put: put is used for update operations in webapi. It can be public void Put (int id, [FromBody] Model) {}; when jq requests a put method from webapi, only $. the jq code is as follows:

               $.ajax({                    type:"put",                    url:"...?id=1",                    data:JSON.stringify(data),                    contentType: 'application/json',                    async:true,                    success:function(){                                            }                });            

 

In this way, the model data of the complex type is transmitted in the json format by marking a contentType: 'application/json' transmission type as json, while the simple type id is transmitted through the url address; this makes it much easier to update data;

Delete: The delete method is used to delete data. The request method is similar to the get method. The method can only be $. the post () method is requested. The Code is as follows: C # code public void Delete (int id ){};

                $.ajax({                    type:"delete",                    url:"...?id=1",                    async:true,                    success:function(){                                            }                });

The focus is on the type of the request, which can only be type: "delete", and the uploaded value can be placed on the url;

The basic operations of jq requests to webapi are here. If there is anything wrong or bad, I hope to point it out. Thank you;

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.