使用 jquery 获取 bing web 结果的 Jsonp 请求
以此作为指导: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将 do jsonp 与 jQuery 结合使用,请将
JsonCallback=UserCallback
替换为JsonCallback=?
。然后 jQuery 将像处理常规 $.ajax()
请求一样处理它。我建议从
$.getJSON()
开始获取习惯了 Bing API 并返回到$.ajax()
当您准备好将其与您的应用程序集成时。使用 Bing API 文档中的示例:
To use do jsonp with jQuery, replace the
JsonCallback=UserCallback
withJsonCallback=?
. 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:
jsonp:
需要设置为一个字符串(我认为也可以省略),因为这只是用于接收 JSONP 的动态创建函数的名称。但是形参 $callBack 需要是对函数的引用,所以要么使用
要么
并且你知道,过度使用 $ 真的很伤我的眼睛:)
另外,以大写开头的函数名应该保留给“类”,因为许多语法检查器会抱怨调用大写字母的函数时前面没有
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
or
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..