Forum Discussion

PerBonomi's avatar
9 years ago

get responseText with Freemarker

Hello.

 

I have an endpoint that spits out a simple number as html.

 

I can use Ajax to get that number, but how do I get it in FM?

 

This works fine, for JS, but how do I get the variable returned as a FM variable, instead?

$.ajax({
	url: '/spotify/plugins/custom/spotify/spotify/my_custom_endpoint',
	error: function() {
		console.log('error')
	},
	success: function(responseText) {
		console.log(responseText)
   },
   type: 'GET'
});

I tried several http client response variants, but no success.

  • Hi Per,

    Calling a Lithium endpoint from within Lithium should be approached like issuing an AJAX call.

    In that regard a http.client.request("url") call should do the job. What#s going wrong when you are trying it that way? What's the error you are seeing?

     

    But: Why do you want to call an endpoint from within Freemarker at all? It sounds like a macro would be more suitable here. If you need to obtain the same data within Freemarker and per JS in the UI you can share that macro both in your component and within the endpoint then.

    • PerBonomi's avatar
      PerBonomi
      Boss

      That is actually a very good point. I'll go the macro route (hardly ever use those..oO)

       

      If you're interested:

       

      When I load my endpoint, the html looks like this:

      <html><head></head><body>70</body></html>

      When I run my ajax I get back 70.

       

      But if I try FM:

      <#assign test = http.client.request("/spotify/plugins/custom/spotify/spotify/endpoint_test")/>
      ${test}

       

      I get this:

      FreeMarker template error (HTML_DEBUG mode; use RETHROW in production!)
      
      
      An error has occurred when reading existing sub-variable "client"; see cause exception! The type of the containing value was: extended_hash+string (lithium.coreapi.webui.template.models.HttpTemplateModel wrapped into f.e.b.StringModel)
      
      ----
      FTL stack trace ("~" means nesting-related):
      	- Failed at: #assign test = http.client.request("/...  [in template "StringDisplay" at line 1, column 1]

      I tried different suffixes like .get().content and such, but since the issue seems to be with the "client" part, that couldn't be the problem.

       

      Anyway, thanks for the feedback!

      • kandulmadhu's avatar
        kandulmadhu
        Advisor

        Hi PerBonomi,

         

        While printing the output, you could try using:

        ${test.content()} / ${test.content}

         

        May be this could work.

         

        Regards,

        Madhu