-1

I am using Spring Boot for a quiz project. I am doing an ajax request for loading the questions and want to call a javascript function from inside a controller like in Ruby on Rails.

What solution offers Spring Boot to do so? Is that the right way? Or is there a better solution? I couldn't find anything.

PrestigeDev
  • 567
  • 8
  • 20
  • You can call several scripting languages from Java, Javascript being one of them. Those script languages can then access your Java data model. Is this what you are looking for ? – Marged Dec 29 '15 at 00:01
  • Instead of responding with a .html template in the Spring Boot controller, I want to return Javascript. – PrestigeDev Dec 29 '15 at 00:05
  • So you are not `calling` javascript but `returning` it. If the Javascript is static you can put it into the resources folder of your project, Boot will happily provide that file. If it is dynamic you can have any controller output any string / text. So you can even return Javascript. – Marged Dec 29 '15 at 00:07
  • Maybe do you mean JSONP: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-jsonp ? – Artem Bilan Dec 29 '15 at 00:21
  • I have a fix Javascript with several methods to be called when a certain action occurs from inside the controller. Sorry for mixing up call and return, but I definitely need to call. – PrestigeDev Dec 29 '15 at 00:31
  • No, I don't mean JSONP. – PrestigeDev Dec 29 '15 at 00:33
  • Java can interoperate with [local JS files](http://stackoverflow.com/q/22856279/1240557), but it's most likely not what you want. Calling JS functions on a page from your Java code directly is not possible, as asked on SO already: http://stackoverflow.com/a/1999559/1240557, http://stackoverflow.com/q/32903641/1240557 or http://stackoverflow.com/q/31475602/1240557. Also, this is not Spring Boot-specific. – kryger Dec 29 '15 at 14:00

1 Answers1

0

I realised how to do it. I needed a rest controller which returned an object. That object is automatically a JSON Object, which can be used afterwards. I can now interact with the quiz answers etc.

PrestigeDev
  • 567
  • 8
  • 20