3

I have a Javascript component that when the DOM is loaded it needs to send a request out to our CDN, which may be in a different domain, to see if there is content for this component. If there is, the component will self-instantiate (its a link to open an embedded video in a modal), if not it will self destruct. My question is mainly about the Grails controller I am using to proxy the AJAX request.

Here is the JS in pseudocode:

checkForVideoAssets: function(videoDataUrl){
    Ajax.get(videoDataUrl, function(data){
    if(data.responseText==='error'){
    //tear down the component
    }
    else{
    //if there is data for the video instantiate the component
    }

Here is the Grails controller:

def checkForModalVideoAsset = {
    def req = new URL("http://" + params.videoUrl + "/expense/videos/")
    def connection = req.openConnection()

    if(connection.responseCode != 200){
        render 'error'
    }

    if(connection.responseCode == 200){
        render req.getText()
    }
}

So, to sum up, the JS grabs an attribute from the DOM that has part of a URL (that we define by convention), sends that URL to the controller, the controller attempts to connect to that URL (at our CDN) and then passes that response back to the AJAX success callback inside the responseText part of the XHR object. This feels less than ideal to me, is it possible to pass the actual response back up to the JS function?

jasongonzales
  • 1,587
  • 2
  • 15
  • 23

1 Answers1

1

The httpbuilder may be usefull to you

I never tried it but something similar!?

def checkForModalVideoAsset = {

    def http = new HTTPBuilder("http://" + params.videoUrl )

    http.get( 
          path : "/expense/videos/",
          contentType : TEXT ) { resp, reader -> 
             response.properties=resp.properties //<-- to easy to work but why not try :)
             response << resp
             }
    }
Fabiano Taioli
  • 5,270
  • 1
  • 35
  • 49