简体   繁体   English

Play Framework 2.2-进行Ajax调用时处理Url参数

[英]Play Framework 2.2 - Handling Url parameters when making ajax call

I have a listing page to delete profiles, if the profile is deleted I need to redirect to the same page and display a message "Profile deleted". 我有一个用于删除个人资料的列表页面,如果个人资料被删除,我需要重定向到同一页面并显示消息“个人资料已删除”。

If it was a regular form I'd use Play Framework's @helper.form and invoke the action in the controller. 如果它是常规形式,我将使用Play Framework的@ helper.form并在控制器中调用该动作。 When that's the case: 在这种情况下:

In my action method I use Redirect(controller.action).flashing("message" -> "some message") and it works fine. 在我的操作方法中,我使用Redirect(controller.action).flashing("message" -> "some message") ,它可以正常工作。 I also use Ok(views.html.profile("some message",filledForm)) if I need to show the page with existing content, and it works fine. 如果我需要显示包含现有内容的页面,我也可以使用Ok(views.html.profile("some message",filledForm)) ,它可以正常工作。

However, since this is just a list I added a button and then perform an ajax call from javascript: 但是,由于这只是一个列表,所以我添加了一个按钮,然后从javascript执行ajax调用:

If it was successful then I redirect using: window.location.replace("/profile/prof_deleted"); 如果成功,那么我使用以下命令重定向: window.location.replace("/profile/prof_deleted"); I use the message (/prof_deleted) to show confirmation/error in profile template. 我使用消息(/prof_deleted)在配置文件模板中显示确认/错误。 However, if the user tries reloading the page, the confirmation/error will be displayed again since it is included in the url. 但是,如果用户尝试重新加载页面,则确认/错误将再次显示,因为它包含在URL中。

I need to pass the message but I think a session is not a good option in this case, flashing is not an option here and url parameter will always displayed. 我需要传递消息,但我认为在这种情况下,会话不是一个好的选择,此处闪烁不是一个选项,并且url参数将始终显示。

It doesn't make sense, if you are using AJAX for sending request for deleting object why do you want to perform common page redirect after all ? 这没有任何意义,如果您使用AJAX发送删除对象的请求,为什么还要执行通用页面重定向?

You have two better options for this scenario (I prefer second one) 对于这种情况,您有两个更好的选择(我更喜欢第二个)

  1. Don't use AJAX, just common POST request with the same route as GET ie: /profiles then when sending POST it deletes object and shows message, otherwise shows list of profiles only. 不要使用AJAX,只需使用与GET具有相同路由的普通POST请求即可: /profiles然后在发送POST时删除对象并显示消息,否则仅显示配置文件列表。
  2. Use AJAX properly: at success state just remove the item from HTML using JavaScript, display message box also with JavaScript but do NOT reload the whole page. 正确使用AJAX:在success状态下,只需使用JavaScript从HTML中删除该项目,还使用JavaScript显示消息框,但不要重新加载整个页面。
    • For deleting required item you just need to give them unique ID, ie: profile_123 要删除必填项,您只需为其提供唯一的ID,即: profile_123
    • There are some ready-to-use, nice libs for displaying info messages via JS, ie: noty 有一些现成的,不错的库,用于通过JS显示信息消息,即: noty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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