This time, I implemented a method similar to ajax call in jQuery, and modified and integrated RESTFul. The A2D AJAX interface implemented is as follows:
$.ajax.RESTFulGetCollection("/api/Users", (data) { alert(data[5"/api/Users/1", "/api/Users/1", (data) { alert("deleted""/api/Users", { UserID: 0, FirstName: "aa", LastName: "bbb"}, (data) { alert("added""/api/Users/1", { UserID: 0, FirstName: "aa", LastName: "bbb"}, (data) { alert(data); });
Let's go deeper. What if someone maliciously accesses the Delete url? For example, CSRF attacks. Common webform or MVC can easily prevent attacks. For more information, see section A8 in this article.
Answer: Use the http header to pass the token. The procedure is as follows (the combination of A2D and ASP. net webapi is used as an example ):
cookieToken + ":" +="@TokenHeaderValue()" $.ajax.RESTFulGetCollection("/api/Users", (data) { alert(data[5"/api/Users/1", "/api/Users/1", (data) { alert("deleted""/api/Users", { UserID: 0, FirstName: "aa", LastName: "bbb"}, (data) { alert("added""/api/Users/1", { UserID: 0, FirstName: "aa", LastName: "bbb"}, (data) { alert(data); });
Then write the webapi handler:
System.Threading.Tasks.Task<HttpResponseMessage> (request.Method == HttpMethod.Post ||== HttpMethod.Put ||== { ValidateRequestHeader(request); } cookieToken = formToken = <> (request.Headers.TryGetValues(, { [] tokens = tokenHeaders.First().Split( (tokens.Length == = tokens[= tokens[
Finally, register the handler in WebApiConfig.
()); config.Routes.MapHttpRoute( name: { id =
All right, all done.
Code download.