0

Is there any way to call @controllers.app.test() by the Javascript?

It has been working fine while using

location.href = '@controllers.app.test()'

And somehow this location.href became an actual URL:

127.0.0.1:9001/admin/@controllers.app.test()

Can anyone help me out with this?

Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46
  • You probably require this: https://www.playframework.com/documentation/2.6.x/ScalaJavascriptRouting ? – Evgeny Mar 17 '18 at 13:10

1 Answers1

0

The @controllers.app.test() is calculated on the server side. Your HTML is in the view and processed by the server, that's why it becomes /admin/@controllers.app.test(). Your Javascript is just in a public folder, so it does not process.

What you can do one of the next:

  1. Put JS file into the view (the same place where your HTML that "works fine" is placed).
  2. Add JS object to the HTML like <script> var myAction="@controllers.app.test()"</script> and then use myAction in JS.
  3. Use Java Script router:

    3.1. https://www.playframework.com/documentation/2.6.x/ScalaJavascriptRouting

    3.2. Convert a javascript variable to scala in play framework

    3.3. How to update src of a img from javascript in a play framework project?

Andriy Kuba
  • 8,093
  • 2
  • 29
  • 46