Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add Req.ajax_? function to identify ajax requests. #1252

Merged
merged 2 commits into from

2 participants

@Shadowfiend
Owner

Ajax requests are identified by looking for the X-Requested-With header to be set to “XMLHttpRequest”. This can be used for the purposes of, for example, deciding whether to render a layout based on the nature of the incoming request.

@Shadowfiend Shadowfiend was assigned
@Shadowfiend Shadowfiend Add ajax_? to Req to tell whether the req was tagged as ajax.
Identification is done using the X-Requested-With header.
4cc150c
@Shadowfiend Shadowfiend commented on the diff
web/webkit/src/main/scala/net/liftweb/http/Req.scala
@@ -847,6 +847,18 @@ class Req(val path: ParsePath,
}
/**
+ * Returns true if the X-Requested-With header is set to XMLHttpRequest.
+ *
+ * Most ajax frameworks, including jQuery and Prototype, set this header
+ * when doing any ajax request.
+ */
+ def ajax_? =
@Shadowfiend Owner

Set this up as a def, which seems to be in keeping with a lot of other stuff in Req, but let me know if it makes more sense as a lazy val.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Shadowfiend Shadowfiend Handle potential multiple values for X-Requested-With.
If any are XMLHttpRequest, we return true for ajax_?.
fca7cdf
@dpp
Owner

Awesome!!

@dpp dpp merged commit 07c008d into master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 7, 2012
  1. @Shadowfiend

    Add ajax_? to Req to tell whether the req was tagged as ajax.

    Shadowfiend authored
    Identification is done using the X-Requested-With header.
  2. @Shadowfiend

    Handle potential multiple values for X-Requested-With.

    Shadowfiend authored
    If any are XMLHttpRequest, we return true for ajax_?.
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 0 deletions.
  1. +12 −0 web/webkit/src/main/scala/net/liftweb/http/Req.scala
View
12 web/webkit/src/main/scala/net/liftweb/http/Req.scala
@@ -847,6 +847,18 @@ class Req(val path: ParsePath,
}
/**
+ * Returns true if the X-Requested-With header is set to XMLHttpRequest.
+ *
+ * Most ajax frameworks, including jQuery and Prototype, set this header
+ * when doing any ajax request.
+ */
+ def ajax_? =
@Shadowfiend Owner

Set this up as a def, which seems to be in keeping with a lot of other stuff in Req, but let me know if it makes more sense as a lazy val.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ request.headers.toList.exists { header =>
+ (header.name equalsIgnoreCase "x-requested-with") &&
+ (header.values.exists(_ equalsIgnoreCase "xmlhttprequest"))
+ }
+
+ /**
* Make the servlet session go away
*/
def destroyServletSession() {
Something went wrong with that request. Please try again.