Spring MVC - JSON 响应 - 如何预防
我有一个前瞻表单字段,它根据名字和姓氏表单字段搜索人员记录。字段由 Jquery 发布,人员列表以 JSON 形式返回,结果显示为表格。为每个 keyup 事件提交 Post 请求。但我对此有疑问。
比方说,用户在名字字段中输入了“a”。该请求作为 ajax 调用提交。然后用户在字段中输入“b”。现在,请求再次以“ab”作为名字提交。问题是,“ab”的结果数量较少,因此结果会立即显示。由于“a”有更多结果,因此需要一些时间来处理和构建结果表,一旦构建表完成,它将替换“ab”的现有表。这不是我想要的行为,因为“a”是第一个请求,“ab”是最新请求,它不应该被替换。那么如何防止旧请求的结果取代新结果。提前致谢!
---更新---
控制器方法类似于..
public @ResponseBody List<PSPerson> getPersonsWithNames(
@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName)
{
Map<String, String> attrMap = new HashMap<String, String>();
attrMap.put("firstName", firstName);
attrMap.put("lastName", lastName);
return personService.getPeople(attrMap);
}
I have a look-ahead form field that searches for people records based on the first name and lastname form fields.. Fields are posted by Jquery and The person list is returned as JSON and results are displayed as a table. Post request is submitted for every keyup event. But I am having an issue with this.
Lets say, user entered 'a' in firstname field. The request is submitted as ajax call. Then user enters 'b' in the field. Now the request is again submitted with 'ab' as firstname. Issue is that, 'ab' has less number of results so the results are displayed instantly. Since 'a' has more results it takes some time to process and build the results table and once it is done building the table, it replaces the existing table for 'ab'. This is not the behavior I want as 'a' was the first request and 'ab' was the latest request, it shouldn't be displaced. So how do I prevent results from old request displacing new results. Thanks in advance!
---Update---
controller method is something like..
public @ResponseBody List<PSPerson> getPersonsWithNames(
@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName)
{
Map<String, String> attrMap = new HashMap<String, String>();
attrMap.put("firstName", firstName);
attrMap.put("lastName", lastName);
return personService.getPeople(attrMap);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以向返回的 Json 值添加时间戳。并在 jquery 中将收到的最新时间戳与实际时间戳进行比较。
Maybe you can add a timestamp to your returned Json values. And in your jquery compare the latest timestamp received to the actual.
您在网络上看到的许多前瞻/自动完成表单都有字符的“阈值计数”(例如,在 PengoWorks jQuery Autocomplete 中,它是
minChars
)。如果您使用该 jQuery 插件,您还可以使用
delay
选项来指定发送 AJAX 请求之前等待的毫秒数。如果您使用典型的
minChars
值(例如3
),加上合理的delay
值(大约 100 毫秒),您可能会“缩小”范围搜索空间足够大,不会出现一个结果集“超越”另一个结果集的任何问题。您可以在此处尝试使用这些值。
编辑:相同的选项在 jQueryUI 中可用 - 它们称为
minLength
和延迟
。A lot of the look-ahead/autocomplete forms that you see on the web have a "threshold count" of characters (e.g. in PengoWorks jQuery Autocomplete it's
minChars
, ).If you're using that jQuery plugin you can also use the
delay
option to specify the number of milliseconds to wait before sending the AJAX request.If you use a typical
minChars
value such as3
, plus a reasonabledelay
value of perhaps 100ms, you will probably have "narrowed down" the search space enough to not have any problems with one resultset "overtaking" another.You can try playing with these values here.
Edit: The same options are available in jQueryUI - they are called
minLength
anddelay
.