Where is isAjax () method in Play Framework 2.0?

In Play Framework 1.x, we could quickly find out if there was an AJAX request or not by requesting the request.isAjax() method, but I cannot find it in Play 2.

What is the alternative or where is this method?

+6
source share
3 answers

If you use jQuery for Ajax, it sets the following request header:

  X-Requested-With: XMLHttpRequest 

that you can later access the test if the call is made through Ajax or not.

+5
source

You can define it as a method and use it in your controller:

 def isAjax[A](implicit request : Request[A]) = { request.headers.get("X-Requested-With") == Some("XMLHttpRequest") } 
+4
source

Here is a naive Java implementation for those not using Scala (like me)

 /** * Check if the request was made via Ajax or not * @return */ public static Boolean isAjax() { String requestWithHeader = "X-Requested-With"; String requestWithHeaderValueForAjax = "XMLHttpRequest"; String[] value = request().headers().get(requestWithHeader); return value != null && value.length > 0 && value[0].equals(requestWithHeaderValueForAjax); } 
+2
source

Source: https://habr.com/ru/post/914201/


All Articles