Last active
December 14, 2015 10:49
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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