在这种情况下使用异步请求是一个好习惯吗?
我有一个场景,JSP 页面中有一个发送电子邮件的按钮,请求使用 jQuery Ajax 和 JSON 异步发送到 servlet,servlet 在数据库中搜索,如果用户有电子邮件,则返回电子邮件地址并向其发送电子邮件,然后转发到发送电子邮件成功或失败的结果页面,但在用户没有电子邮件的情况下,它使用 JSON 到 JSP 返回 false 值,然后JSP 表单似乎用户输入他的电子邮件。
使用 Ajax 是一种好的做法吗?我知道不是每次都有返回值给用户或使用 get 方法向 servlet 发送请求,该方法在案例中返回一个参数用户没有电子邮件?
I have a scenario that I have a button in JSP page which sends an email, the request is send to servlet asynchronously using jQuery Ajax and JSON, servlet searches in DB, if the user has an email, it returns the email address and sends an email to it, then forwards to the result page with success or fail of sending the email, but in a case that the user doesn't have an email, it returns false values using JSON to JSP and then a JSP form appears to the user to enter his email.
Is it good practice to use Ajax and I know that not each time there's a return value to the user or send request to servlet using get method which return a parameter in a case that the user doesn't have an email?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几乎在所有情况下,使用 ajax 都非常有利于用户体验。使用 ajax,用户将体验即时反馈,而无需面对烦人的“内容闪现”或(部分)空页面,因为整个 HTML 响应需要首先由服务器生成/缓冲。这确实是使用 JS/ajax 的一个巨大优势。
使用 JSON 通常比 XML、HTML 甚至纯文本更受欢迎。但是对于客户端和服务器之间的ajax数据交换格式并没有“最佳实践”。只需选择最适合要求的即可。 JSON 非常适合这种情况。 jQuery 可以直接理解它,而在 Java 中,您可以选择大量易于使用的 JSON 解析器。
然而,在开发支持 ajax 的 Web 应用程序时,您确实需要考虑到当客户端 禁用 JS 时,核心功能不会中断。这称为Unobtrusive JavaScript。大多数搜索机器人、移动浏览器和基于文本的浏览器不使用 JS。您应该尝试仅将 JS 用于渐进式增强。要亲自测试这一点,在 Firefox 中,您可以使用 Web 开发人员工具栏 轻松启用/禁用 JS 支持。在禁用 JS 的情况下运行您的网站,并观察核心功能是否也得到维护。
实现这一目标的最佳方法是开始开发网站,不需要任何一行 JS 代码,甚至不需要任何
onclick
、onsubmit
、onwhatever
HTML 元素上的属性。一旦核心功能开始工作,您就可以开始添加脚本风格的 JS,该脚本在文档准备期间执行,并将函数附加到感兴趣的 HTML 元素(即使在这里,您也不应该不更改原始 HTML 代码!)。让 JS 函数在同一 URL 或不同的 URL 上触发 ajax 请求,具体取决于需求。您可以在 Servlet 中区分 ajax 和普通请求,如下所示:另请参阅:
Using ajax is in practically all cases very good for User Experience. With ajax, the user will experience instant feedback without the need to face an annoying "flash of content" or a (partially) empty page because the whole HTML response needs to be generated/buffered by the server first. This is really a huge plus of using JS/ajax.
Using JSON is generally favorable above XML, HTML or even plain text. But there is no "best practice" with regard to the ajax data exchange format between client and server. Just pick whatever suits the requirement the best. JSON is perfectly fine for this case. jQuery understands it out-the-box and in Java you have choice of a plethora of easy-to-use JSON parsers.
However, when developing an ajax-enabled webapplication, you really need to take into account that the core functionality does not break when the client has JS disabled. This is called Unobtrusive JavaScript. Most of the searchbots, mobile browsers and textbased browsers don't use JS. You should try to use JS only for Progressive Enhancements. To test this yourself, in Firefox you can use for example the Web Developer Toolbar to easily enable/disable JS support. Run your website with JS disabled and observe if the core functionality is maintained as well.
The best way to achieve this is to start developing the website without any single line of JS code, even without a single
onclick
,onsubmit
,onwhatever
attribute on the HTML elements. Once you get the core functionality to work, then you can start adding JS in flavor of a script which executes during document ready and attachs functions to the HTML elements of interest (even here, you should not change the original HTML code!). Let the JS functions fire ajax requests on the same URL or maybe a different one, depending on the requirement. You can in the Servlet distinguish between an ajax and normal request as follows:See also:
Json 只是一种数据交换格式。使用或不使用 Json 与是否使用异步通信无关...您可以使用 Json (或 XML,或序列化对象,这并不重要)来执行两种通信类型。
现在,在您的问题中,看起来您只是想使用异步通信来改善用户体验(它不会轻弹用户的浏览器)。如果是这样的话,异步通信就是正确的选择!
Json is just a data-interchange format. Using Json or not has nothing to do with using asynchronous communication or not... You can do both communication types using Json (or XML, or serialized objects, it doesn't matter).
Now, in your problem, it looks like you just want to use Asynchronous communication to improve the user experience (it will not flick the user's browser). If that's the case, Asynchronous communication is the way to go!
我认为你不需要在这方面使用 AJAX。
ajax 的主要思想是在没有回发的情况下呈现服务器响应,在您的情况下,您在获得某种结果后重定向页面。
我认为你应该选择这两种方式。
1)使用AJAX,将数据发送到servlet,然后无论邮件是否发送,都从服务器呈现响应。
2) 将表单提交到 servlet 并发送电子邮件,然后重定向到包含成功/失败结果的 jsp。
希望有帮助。
I don't think you need ot use AJAX in this.
The main idea of the ajax is to render server response without postback and in your case you are redirecting page after you get some kind of result.
In my opinion you shoul choose on of these two ways.
1) Use AJAX, send data to servlet and then render response from server wether the mail is sent or not.
2) Submit your form to servlet and sent email and then redirect to jsp with the success/fail result.
Hope it helps.