Bing search API using Jsonp not working, invalid label

依然范特西╮ 提交于 2019-12-13 14:09:28

问题


Struggling with Bing's json request (bing search, not map), I am getting an error back that says 'Invalid Label'

My query url is:

var bingurl="http://api.search.live.net/json.aspx?Appid=##APIKEY##&query=Honda&sources=web";


 $.ajax({
            type: "GET",
            url: bingurl,
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "jsonp",
            success: function(data) {

                $callBack(data);
            },
            error: function(msg) {
                alert("error" + msg);
            }
        });

Firebug reports 'invalid label' and then dumps the json response.

No idea what is wrong? help appreciated.


回答1:


The Bing API URL you posted isn't JSONP, it's plain JSON.

JSONP is interpreted as raw JavaScript, in which case a JSON object's {"something": ... syntax is not an object literal, but a block statement with a label whose name contains quotes (hence the invalidness).

As I understand it, if you want JSONP from Bing you have to tell it that by passing in parameters ...&JsonType=callback&JsonCallback=(name of global callback function).

(I'm also not sure what data: "{}" will do, but I don't think anything good.)




回答2:


Just in the spirit of keeping things up-to-date, the newer Bing REST API does support jsonp, you just have to make sure that the "callback" parameter is "jsonp". In jQuery just change the jsonp attribute in your $.ajax() call to "jsonp" to make this work.

$.ajax({
    url: 'http://some.domain.com',
    dataType: 'jsonp',
    jsonp: 'jsonp'
});`


来源:https://stackoverflow.com/questions/2829750/bing-search-api-using-jsonp-not-working-invalid-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!