如何通过回调函数传递参数来搜索 Yahoo BOSS 和 BING 等 API?

发布于 2024-10-18 01:24:04 字数 205 浏览 1 评论 0原文

我正在使用 Yahoo BOSS 和 Bing API 为我的网站提供搜索功能。具体来说,我使用他们的 JSON 响应格式,将回调函数传递给搜索提供程序,该函数稍后将随搜索结果一起回调。我的回调函数实际上被调用,但问题是,如果我一次发出多个请求,我无法判断某个响应是针对哪个请求的。为此,有没有办法将回调函数的附加参数传递给搜索提供程序,以便我稍后可以使用它来识别哪个响应与哪个请求相关? 谢谢

I am using Yahoo BOSS and Bing APIs to provide search functionality to my site. Specificaly, I use their JSON response formats where I would pass a callback function to the search provider that would later be called back with the search results. My callback function actually gets called, but the problem is, if I make more than one requests at a time, I can't tell which request a certain response is for. To this end, is there a way to pass additional parameters with the callback function to the search provider so that I can later use it to identify which response goes with which request?
Thank you

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

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

发布评论

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

评论(1

一曲琵琶半遮面シ 2024-10-25 01:24:04

我和你有同样的问题!我用谷歌搜索并找到了一些解决方案
我已经解决了我的问题。现在我向您展示它,希望它可以帮助您:)

之前的代码:

       function MakeGeocodeRequest(credentials) {
        var pins = checkLocation.d
        $.each(pins, function (index, pin) {
            var geocodeRequest = 'http://ecn.dev.virtualearth.net/REST/v1/Locations/' + pin.City + ',' + pin.Country + '?output=json&jsonp=GeocodeCallback&key=' + credentials;
            CallRestService(geocodeRequest);
        });



    function CallRestService(request) {
        var script = document.createElement("script");
        script.setAttribute("type", "text/javascript");
        script.setAttribute("src", request);
        document.body.appendChild(script);
    }

function GeocodeCallback(result) {.. to do with result callback, -->我想在这里添加一些 pin 信息}

因为每个 sccipt 添加到文档 ( document.body.appendChild(script);) 时都会运行 -->和回调,你不能添加更多参数。

我通过ajax请求解决它(不再添加到文档中),当ajax调用成功时 -->我调用 GeocodeCallback(result, pin)
这是完整的代码。

   function MakeGeocodeRequest(credentials) {
        var pins = checkLocation.d;
        $.each(pins, function (index, pin) {
            $.ajax({
                url:"http://ecn.dev.virtualearth.net/REST/v1/Locations/",
                dataType: "jsonp",
                data:{key:credentials,q:pin.City + ',' + pin.Country},
                jsonp:"jsonp",
                success: function(result){
                    GeocodeCallback(result,pin);
                }
            });
        });
    }
    function GeocodeCallback(result,pin) { ... to do here}

I have a same problem with you! I googled and find some solutions
and I has solve my problem. Now i show it to you, I hope it can help you :)

Previous code:

       function MakeGeocodeRequest(credentials) {
        var pins = checkLocation.d
        $.each(pins, function (index, pin) {
            var geocodeRequest = 'http://ecn.dev.virtualearth.net/REST/v1/Locations/' + pin.City + ',' + pin.Country + '?output=json&jsonp=GeocodeCallback&key=' + credentials;
            CallRestService(geocodeRequest);
        });



    function CallRestService(request) {
        var script = document.createElement("script");
        script.setAttribute("type", "text/javascript");
        script.setAttribute("src", request);
        document.body.appendChild(script);
    }

function GeocodeCallback(result) {.. to do with result callback, --> i want to add some pin infomation here}

Because each sccipt when add to document ( document.body.appendChild(script);) it will be run --> and callback, you cant add more params.

I solve it by request through ajax (doesnt add to document any more), when the ajax call success --> I call the GeocodeCallback(result, pin)
Here is the complete code.

   function MakeGeocodeRequest(credentials) {
        var pins = checkLocation.d;
        $.each(pins, function (index, pin) {
            $.ajax({
                url:"http://ecn.dev.virtualearth.net/REST/v1/Locations/",
                dataType: "jsonp",
                data:{key:credentials,q:pin.City + ',' + pin.Country},
                jsonp:"jsonp",
                success: function(result){
                    GeocodeCallback(result,pin);
                }
            });
        });
    }
    function GeocodeCallback(result,pin) { ... to do here}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文