A2D JS framework

Source: Internet
Author: User

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.

 

 

 

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.