Spring MVC - JSON 响应 - 如何预防

发布于 2024-12-12 04:43:44 字数 755 浏览 0 评论 0原文

我有一个前瞻表单字段,它根据名字和姓氏表单字段搜索人员记录。字段由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

子栖 2024-12-19 04:43:44

也许您可以向返回的 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.

往事风中埋 2024-12-19 04:43:44

您在网络上看到的许多前瞻/自动完成表单都有字符的“阈值计数”(例如,在 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 as 3, plus a reasonable delay 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 and delay.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文