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;