Skip to content

Instantly share code, notes, and snippets.

@farmdawgnation
Last active December 14, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farmdawgnation/5074718 to your computer and use it in GitHub Desktop.
Save farmdawgnation/5074718 to your computer and use it in GitHub Desktop.
A Lift response transformer and some CoffeeScript helpful for facilitating cross-domain AJAX requests in Lift.
// If we receive a jQuery JSONP call and we don't call the callback, add in a bogus
// call to the callback function.
LiftRules.responseTransformers.append { response =>
response.toResponse match {
case resp @ InMemoryResponse(data, headers, _, _) =>
{
for(callback <- S.param("callback")) yield {
val dataStr = new String(data)
if (! dataStr.contains(callback)) {
val updatedData = dataStr + "\n" + callback + "();"
val updatedHeaders = headers.collect {
case (headerName, value) if headerName == "Content-Length" =>
(headerName, updatedData.length.toString)
case otherHeader => otherHeader
}
resp.copy(data = updatedData.getBytes("UTF-8"), headers = updatedHeaders)
} else {
resp
}
}
} openOr {
resp
}
case _ =>
response
}
}
window.liftAjax.lift_actualAjaxCall = (data, version, onSuccess, onFailure) ->
hostnamePrefix = ""
# Replace the following with values appropriate for your production hostname.
if window.location.hostname == "anchortab.com"
hostnamePrefix = "https://anchortab.com"
jQuery.ajax
url : hostnamePrefix + liftAjax.addPageNameAndVersion("/ajax_request/", version),
data : data,
dataType : "jsonp",
success : onSuccess,
error : onFailure,
crossDomain: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment