根据请求验证 ajax 响应

发布于 2024-10-26 15:45:44 字数 359 浏览 2 评论 0原文

ajax 和/或 jquery 中是否有某种方法可以验证我收到的响应是否针对我发送的特定请求?

为了说得更清楚。我有一个搜索页面,分为两部分,顶部部分是搜索条件部分,第二部分是匹配结果。默认情况下,会获取前 20 条记录。结果部分通过 ajax 请求定期更新。用户可以选择过滤提供的结果。这也是通过 ajax 发生的。

两个请求的 URL 相同。唯一的区别是当用户选择过滤结果时附加到 URL 的过滤条件。

由于用户过滤器的优先级高于后台刷新,因此当检测到用户请求特定文件管理器时,我需要中止后台刷新。是否有某种方法可以检查 ajax 响应并验证该响应是否对应于后台请求或用户特定请求?

我已尽力解释该查询,但如果您需要更多信息,请告诉我。

Is there some means in ajax and/ or jquery by which I can validate if the response I have received is for the specific request that I sent?

To make it more clear. I have a search page thts divided into two parts, the top part is the search criteria section and the second part is the matched results. By default the 1st 20 records are fetched. The results section is periodically updated via an ajax request. The user can choose to filter the provided results. This too happens via ajax.

The URL for both the requests is the same. The only difference being the filter criteria appended to the URL when the user chooses to filter the results.

Since the users filter has higher precedence than the back ground refresh I need to abort a background refresh when its detected that the user has requested a specific filer. Is there some means of checking the ajax response and validating that this response corresponds to the back ground request or user specific request?

I have tried my best to explain the query but do let me know if you need any more info.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

倾城花音 2024-11-02 15:45:44

我想我明白你要做什么。我的理解是,页面的一部分会定期自动刷新,但如果用户明确指示了过滤器,您希望这些刷新停止。正确的?

我不知道你的 ajax 响应是什么样的,所以这必须是抽象的,但是
我假设您的 ajax 响应是 HTML 或 JSON。

当您对用户指定的过滤器请求做出响应时,只需添加某种标志来表明这种情况已经发生。

HTML:将其添加到可能添加到页面某处的代码中,使用 innerhtml

<input type="hidden" id="user-defined-filter" value=1 />

JSON:将其添加到响应 JSON 对象。我将其命名为 ajaxResponse1

{user-defined-filter: 1}

然后在自动刷新之前添加一个条件,例如

if (!$("user-defined-filter").val()) {//auto-refresh stuff

OR

if (!ajaxResponse1.user-defined-filter) {//auto-refresh stuff

I think i understand what you're going for. My understanding is that a part of your page auto-refreshes periodically, but if the user has explictly indicated a filter you want these refreshes to stop. correct?

I don't know what your ajax responses are like, so this will have to be kind of abstract, but
I presume your ajax response is either HTML or JSON.

When you make the response for user-specified-filter requests, just add some kind of flag to indicate that this has happened.

HTML: add this to the code that is probably added to your page somewhere w/ innerhtml

<input type="hidden" id="user-defined-filter" value=1 />

JSON: add this to response JSON object. I'll call it ajaxResponse1

{user-defined-filter: 1}

And then put a conditional before your auto-refresh such as

if (!$("user-defined-filter").val()) {//auto-refresh stuff

OR

if (!ajaxResponse1.user-defined-filter) {//auto-refresh stuff
成熟稳重的好男人 2024-11-02 15:45:44

也许您可以保存后台请求的 $.ajax() 返回对象,并根据用户请求中止它:

// global
var bgAjax;

// In you treatment og background
bgAjax = $.ajax(...);

// Abort
bgAjax.abort();

Maybe you can save $.ajax() return object of the background request, and abort it on user request:

// global
var bgAjax;

// In you treatment og background
bgAjax = $.ajax(...);

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