在 js 文件内播放框架控制器操作调用
我正在寻找一种解决方案来调用外部 js 文件内的控制器方法/操作,而不是在 html 文件内!
所以我有一个 test.js 文件,其中有一个 ajax 调用,我想让 play 创建如下 url:
$.ajax({
type: 'post',
url: "#{@Application.sendMail();/}", //i know it's not the right syntax but something like this
.......
});
所以我不知道这是如何工作的。感谢您的建议!
干杯,
马可
I'm looking for a solution to call a controller method/action inside an external js-file, not inside html-file!
so I have a test.js file, which has a ajax call and I want to let play create the url like:
$.ajax({
type: 'post',
url: "#{@Application.sendMail();/}", //i know it's not the right syntax but something like this
.......
});
so I don't know how this works. thanks for your suggestions!
cheers,
Marco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
js 文件是静态文件,因此将变量放入其中并不是一个好习惯,因为这样会释放浏览器和服务器缓存。
在这种情况下,最好在 js 中创建一个函数,并使用 url 作为参数从 html 中调用它
js files are static files so it is not a good practice to put variables inside because you then loose browser and server caching.
In such a situation it is better to create a function in your js and call it from your html with the url as an argument
正如 @SebCesbron 所说,这通常不是一个好主意;不过你仍然可以做到。
为此,请玩!必须将 JS 文件作为视图提供,而不是作为静态文件。
请参阅:https://stackoverflow.com/a/8711690/422670 了解有关如何执行此操作的更多信息。
As @SebCesbron say, it's usually not a very good idea; you can still do it though.
In order to do so, Play! must serve you JS file as a view, not as a static file.
Please see this: https://stackoverflow.com/a/8711690/422670 for more informations on how to do that.