使用 jquery 获取 bing web 结果的 Jsonp 请求

发布于 2024-09-01 17:13:51 字数 1337 浏览 1 评论 0原文

以此作为指导:http://msdn.microsoft.com/en- us/library/dd250846.aspx

有人可以帮我进行 jquery 调用吗?

我实际上是传入回调的 javascript 代码,还是只是函数的名称?

BingSearch = function($bingUrl, $bingAppID, $keyword, $callBack) {

        $bingUrl = $bingUrl + "?JsonType=callback&JsonCallback=" + $callBack + "&Appid=" +                  $bingAppID + "&query=" + encodeURI($keyword) + "&sources=web";


$.ajax({
            dataType: 'jsonp',
            jsonp: $callBack,
            url: $bingUrl,
            success: function(data) {
                alert('success');
                $callBack(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("error: " + textStatus);
            }
        });

};

更新

好的,我将其更改为:

BingSearch = function(bingUrl, bingAppID, keyword, callback) {

    var url = bingUrl + "?method=?&JsonType=callback&Appid=" + bingAppID + "&query=" + encodeURI(keyword) + "&sources=web";

    $.getJSON(url, callback);

};

像这样调用它:

BingSearch(url, appid, searchkeyword, function(searchresults) {

    alert('yes!');
};

仍然收到“无效标签”错误。

using this as a guide: http://msdn.microsoft.com/en-us/library/dd250846.aspx

can someone help me with the jquery call?

Do I actually pass in the javascript code for the callback, or just the name of the function?

BingSearch = function($bingUrl, $bingAppID, $keyword, $callBack) {

        $bingUrl = $bingUrl + "?JsonType=callback&JsonCallback=" + $callBack + "&Appid=" +                  $bingAppID + "&query=" + encodeURI($keyword) + "&sources=web";


$.ajax({
            dataType: 'jsonp',
            jsonp: $callBack,
            url: $bingUrl,
            success: function(data) {
                alert('success');
                $callBack(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert("error: " + textStatus);
            }
        });

};

Update

Ok so I changed this to:

BingSearch = function(bingUrl, bingAppID, keyword, callback) {

    var url = bingUrl + "?method=?&JsonType=callback&Appid=" + bingAppID + "&query=" + encodeURI(keyword) + "&sources=web";

    $.getJSON(url, callback);

};

Calling it like:

BingSearch(url, appid, searchkeyword, function(searchresults) {

    alert('yes!');
};

Still getting the 'invalid label' error.

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

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

发布评论

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

评论(2

匿名的好友 2024-09-08 17:13:51

要将 do jsonp 与 jQuery 结合使用,请将 JsonCallback=UserCallback 替换为 JsonCallback=?。然后 jQuery 将像处理常规 $.ajax() 请求一样处理它。

我建议从 $.getJSON() 开始获取习惯了 Bing API 并返回到 $.ajax() 当您准备好将其与您的应用程序集成时。

使用 Bing API 文档中的示例:

var apikey = 'YOUR_API_KEY';
var url = 'http://api.bing.net/json.aspx?AppId='+apikey+'&Version=2.2&Market=en-US&Query=testign&Sources=web+spell&Web.Count=1&JsonType=callback&JsonCallback=?';
$.getJSON(url, function(data) { console.log(data); });

To use do jsonp with jQuery, replace the JsonCallback=UserCallback with JsonCallback=?. jQuery will then handle it like a regular $.ajax() request.

I suggest starting out with $.getJSON() to get used to the Bing API and moving back to $.ajax() when your ready to integrate it with your application.

Using the example from the Bing API docs:

var apikey = 'YOUR_API_KEY';
var url = 'http://api.bing.net/json.aspx?AppId='+apikey+'&Version=2.2&Market=en-US&Query=testign&Sources=web+spell&Web.Count=1&JsonType=callback&JsonCallback=?';
$.getJSON(url, function(data) { console.log(data); });
旧时光的容颜 2024-09-08 17:13:51

jsonp: 需要设置为一个字符串(我认为也可以省略),因为这只是用于接收 JSONP 的动态创建函数的名称。

但是形参 $callBack 需要是对函数的引用,所以要么使用

function callback(result){ /*processResultHere*/ }

BingSearch(..,..,.., callback);

要么

BingSearch..,..,.., function(result){ /*processResultHere*/ });

并且你知道,过度使用 $ 真的很伤我的眼睛:)

另外,以大写开头的函数名应该保留给“类”,因为许多语法检查器会抱怨调用大写字母的函数时前面没有 new

jsonp: needs to be set to a string (I think it can also be left out), as this is just the name of the dynamically created function used to receive the JSONP.

But the formal parameter $callBack needs to be a reference to a function, so either you use

function callback(result){ /*processResultHere*/ }

BingSearch(..,..,.., callback);

or

BingSearch..,..,.., function(result){ /*processResultHere*/ });

And just so you know it, the excessive use of $ really hurts my eyes :)

Also, function names beginning with a capital should be reserved for 'classes', as many syntax checkers will complain on functions with capitals being called without new in front..

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