YQL - 如何通过自定义 ID 签署请求

发布于 2024-12-28 19:43:02 字数 1280 浏览 4 评论 0原文

我有一个非常简单的 jQuery 应用程序来查询 YQL,

SELECT title, href FROM digg.search.search WHERE query="cats" LIMIT 5

我将每个查询记录到简单的 ul-li 列表中,其中指示 ajax 请求的状态(ajax-loader,好的,错误)。请求完成后,我想将相应li元素的CSS class“loading”更改为“ok”/分别为“错误”。

但为了识别正确的 li 元素,我必须通过某种唯一的 ID 找到它。在 AJAX 调用之前,我通过 data- 属性标记 li-element,如下所示:

var rqid = uniqId();
$searchLogger.append('<li class="loading" data-rqid="'+ rqid +'"><a href="#">' + q + '</a></li>');
$.ajax({
    url     : 'http://query.yahooapis.com/v1/public/yql',
    type    : 'POST',
    dataType: 'json',
    data    : {
        callback : '',
        format   : 'json',
        env      : 'store://datatables.org/alltableswithkeys',
        q        : 'SELECT title, href FROM digg.search.search WHERE query="' + q + '" LIMIT 5'
    }

在成功回调中,我需要根据 li 元素进行引用。我无法重用 rqid 变量,因为当第一个请求仍在等待响应时,它可以被其他请求重写。

success: function(data) {
    $searchLogger.find('[data-rqid="' + idOfThisRequest + '"]')
        .removeClass('loading')
        .removeClass('error')
        .addClass('ok');

    ... // other code
}

YQL 请求是否有任何选项,可以通过我的 rqid“签署”请求,并在回调中获取该 ID?

I have very simple jQuery application quering YQL with

SELECT title, href FROM digg.search.search WHERE query="cats" LIMIT 5

I'm logging each query to simple ul-li list, where I'm indicating state of ajax request (ajax-loader, ok, error). After the request is finished, I want to change CSS class of appropriate li-element from "loading" to "ok"/"err" respectively.

But to identify correct li-element, I must somehow find it by some unique ID. I mark li-element by data- attribute before AJAX call like this:

var rqid = uniqId();
$searchLogger.append('<li class="loading" data-rqid="'+ rqid +'"><a href="#">' + q + '</a></li>');
$.ajax({
    url     : 'http://query.yahooapis.com/v1/public/yql',
    type    : 'POST',
    dataType: 'json',
    data    : {
        callback : '',
        format   : 'json',
        env      : 'store://datatables.org/alltableswithkeys',
        q        : 'SELECT title, href FROM digg.search.search WHERE query="' + q + '" LIMIT 5'
    }

In success callback I need to reference according li element. I cannot reuse rqid variable, cos it can be rewriten by other request while the first is still waiting for response.

success: function(data) {
    $searchLogger.find('[data-rqid="' + idOfThisRequest + '"]')
        .removeClass('loading')
        .removeClass('error')
        .addClass('ok');

    ... // other code
}

Is there any option to YQL request, to "sign" request by my rqid, and to fetch that ID in callback?

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

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

发布评论

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

评论(1

不即不离 2025-01-04 19:43:02

我不相信 YQL 中有任何选项可以将这样一个唯一的 ID 添加到回调中返回的查询中。

由于您使用的是 jQuery,因此您可以执行 jQuery 论坛中所述的操作: 唯一标识ajax请求/响应。基本上,他们会向 XHR 请求添加一个唯一标识符,您可以在回调函数中读取该标识符。

I don't believe there is any option in YQL to add such a unique ID to the query that would be returned in the callback.

Since you are using jQuery, you might be able to do something as described here in the jQuery forums: Uniquely identifying ajax requests / responses. Basically they are adding to the XHR request a unique identifier which you can read in the callback function.

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