简体   繁体   中英

How can i generate ajax url from external javascript file in play framework?

I use the "jsAction" tag to generate ajax url in play framework:

var addToFriendURL = #{jsAction @users.addToFriend(':friendId') /}

The code works fine in html template inside a tag,but i found it's invalid in a external javascript file which i include in the html template.

What was happened when i move the code to a external javascript file ?

Once it is moved to javascript file, it becomes statically served content that the Play server does not parse. Therefore, you have a few options

  1. Move your JS into your View directory and create a controller to render the full JS, which can then use the groovy code and tags to do what you need
  2. Keep the dynamic parts of your javascript inside your view, and leave the rest in external files

Personally I would go with option 2, but it may depend on your requirements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM