OPTIONS
返回服务器针对特定资源所支持的HTTP请求方法。也可以利用向Web服务器发送'*'的请求来测试服务器的功能性。
HEAD
向服务器索要与GET请求相一致的响应,只不过响应体将不会被返回。这一方法可以在不必传输整个响应内容的情况下,就可以获取包含在响应消息头中的元信息。
GET
向特定的资源发出请求。注意:GET方法不应当被用于产生“副作用”的操作中。
POST
向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。POST请求可能会导致新的资源的建立和/或已有资源的修改。
PUT
向指定资源位置上传其最新内容。
DELETE (后台需要实现逻辑)
请求服务器删除Request-URI所标识的资源。(后台需要有实现逻辑)
TRACE
回显服务器收到的请求,主要用于测试或诊断。
CONNECT
HTTP/1.1协议中预
MVC4 webapi实例 post delete put get 就当增删改查来用吧
public class ComplexTypeValuesController : ApiController { static readonly MoviesRepository repository = new MoviesRepository(); // GET api/values public IEnumerableGet() { return repository.Movies; } // GET api/values/5 public IHttpActionResult Get(int id) { Movie repMovie = repository.Movies.Where(m => m.ID == id).SingleOrDefault(); if (repMovie == null) return NotFound(); else return Ok(repMovie); } /* Post this JSON object will cause cross-field validation error from IValidatableObject: { "Title": "Fifty Shades of Grey", "ReleaseDate": "2015-02-13T00:00:00", "Genre": "Family", "Price": 15.99, "Rating": "R", "Stars": 5 } */ // POST api/values public IHttpActionResult Post(Movie movie) { if (!ModelState.IsValid) return BadRequest(ModelState); int? maxId = (from m in repository.Movies orderby m.ID descending select m.ID).FirstOrDefault(); Movie newMovie = new Movie { ID = (maxId != null) ? (int)maxId + 1 : 1, Title = movie.Title, ReleaseDate = movie.ReleaseDate, Genre = movie.Genre, Rating = movie.Rating, Price = movie.Price }; repository.Movies.Add(newMovie); return Ok(newMovie); } // PUT api/values/5 public IHttpActionResult Put(int id, Movie movie) { if (!ModelState.IsValid) return BadRequest(ModelState); var repMovie = (from m in repository.Movies where m.ID == id select m).SingleOrDefault(); if (repMovie == null) return NotFound(); else { repMovie.Title = movie.Title; repMovie.ReleaseDate = movie.ReleaseDate; repMovie.Genre = movie.Genre; repMovie.Rating = movie.Rating; repMovie.Price = movie.Price; return Ok(repMovie); } } // DELETE api/values/5 public IHttpActionResult Delete(int id) { var repMovie = (from m in repository.Movies where m.ID == id select m).SingleOrDefault(); if (repMovie == null) return NotFound(); else { repository.Movies.Remove(repMovie); return Ok(); } } }
js 调用部分:
/// // Simple Type $("#GetAllRequest").click(function (e) { $.ajax({ url: "/api/values", type: "GET", dataType: "json" // aka Accept }) .done(function (data, textStatus, jqXHR) { $('#GetAlloutput').html(data); }) .fail(function (jqXHR, textStatus, errorThrown) { $('#GetAlloutput').html('Bummer: there was an error: ' + textStatus); }); }); $("#GetIdRequest").click(function (e) { $.ajax({ url: "/api/values/42", type: "GET", dataType: "json" // aka Accept }) .done(function (data, textStatus, jqXHR) { $('#GetIdoutput').html(data); }) .fail(function (jqXHR, textStatus, errorThrown) { $('#GetIdoutput').html('Bummer: there was an error: ' + textStatus); }); }); $("#PostRequest").click(function (e) { $.ajax({ url: "/api/values", type: "POST", dataType: "json", // aka Accept contentType: "application/json", data: "\"myData\"" }) .done(function (data, textStatus, jqXHR) { $('#Postoutput').html(data); }) .fail(function (jqXHR, textStatus, errorThrown) { $('#Postoutput').html('Bummer: there was an error: ' + textStatus); }); }); $("#PutRequest").click(function (e) { $.ajax({ url: "/api/values/42", type: "PUT", dataType: "json", // aka Accept contentType: "application/json", data: "\"myData\"" }) .done(function (data, textStatus, jqXHR) { $('#Putoutput').html(data); }) .fail(function (jqXHR, textStatus, errorThrown) { $('#Putoutput').html('Bummer: there was an error: ' + textStatus); }); }); $("#DeleteRequest").click(function (e) { $.ajax({ url: "/api/values/42", type: "DELETE", dataType: "json", // aka Accept contentType: "application/json" }) .done(function (data, textStatus, jqXHR) { $('#Deleteoutput').html(data); }) .fail(function (jqXHR, textStatus, errorThrown) { $('#Deleteoutput').html('Bummer: there was an error: ' + textStatus); }); });