2

我正在尝试使用 jquery.ajax查询http://developer.pintlabs.com/brewerydb/api-documentation 。

这是我尝试过的两个 Jquery 请求(注意“O3tmVI”是一个虚拟 ID):

    $.ajax({
            url:"http://api.playground.brewerydb.com/beer/" + "O3tmVI" + "?key=A1029384756B&format=json",
            dataType: "jsonp",
            jsonpCallback: "callbackfunctie",
            success:function(oData){
                    var returnData = oData;
                    console.log(returnData);     
            }
    });             

和:

$.getJSON("http://api.playground.brewerydb.com/beer/" + "O3tmVI" + "?key=A1029384756B&format=json&jsoncallback=?",
                function(data){
                    console.log(data);
                });

两者都会导致此错误:

Uncaught SyntaxError: Unexpected token :

现在,返回的 json 对象如下所示:

{"message":"Request Successful","data":{"id":"O3tmVI","name":"The Public","description":"The Public\u2122 is a delicious easy drinking pale ale made from a simple recipe of quality grain and top notch American hops. Tawny hues of caramel and amber are a trademark of the Public ale as well as a delicious spruce crispness resulting from a beautiful abundance of hops! This beer will be produced throughout the year and serves as the foundation of our product line.","abv":"6","glasswareId":8,"availableId":1,"styleId":33,"isOrganic":"N","labels":{"icon":"http:\/\/s3.amazonaws.com\/brewerydbapi\/beer\/O3tmVI\/upload_3sdJcU-icon.png","medium":"http:\/\/s3.amazonaws.com\/brewerydbapi\/beer\/O3tmVI\/upload_3sdJcU-medium.png","large":"http:\/\/s3.amazonaws.com\/brewerydbapi\/beer\/O3tmVI\/upload_3sdJcU-large.png"},"status":"verified","statusDisplay":"Verified","servingTemperature":"cool","servingTemperatureDisplay":"Cool - (8-12C\/45-54F)","createDate":"2012-04-05 04:02:36","updateDate":"2012-04-05 04:34:17","glass":{"id":8,"name":"Tulip","createDate":"2012-04-05 04:00:04"},"available":{"id":"1","name":"Year Round","description":"Available year round as a staple beer."},"style":{"id":33,"categoryId":10,"category":{"id":10,"name":"American Ale","bjcpCategory":"10","createDate":"2012-04-05 04:00:04"},"bjcpSubcategory":"A","name":"American Pale Ale","simpleUrl":"american-pale-ale","ibuMin":"30","ibuMax":"45","abvMin":"4.5","abvMax":"6.2","srmMin":"5","srmMax":"14","ogMin":"1.045","ogMax":"1.06","fgMin":"1.01","fgMax":"1.015","createDate":"2012-04-05 04:00:04"}},"status":"success"}

我已成功将其验证为有效的 JSON

现在,问题是我需要一个 JSONP 对象才能请求它(跨域)。所以它必须被包装在一个容器函数中。使用请求中的参数应该可以做到这一点,但是到目前为止,我几乎尝试了任何方法,但都没有成功...

我发现这篇文章有一个关于这个错误的简短条目:http ://www.jquery4u.com/json/ajaxjquery-getjson-simple/

Uncaught SyntaxError: Unexpected token :(in crome) Invalid Lable(in firefox) “invalid label” 错误可以通过将 JSON 数据传递给 js 回调来修复

但我真的不明白他在说什么

我究竟做错了什么?

4

2 回答 2

2

我的猜测是http://api.playground.brewerydb.com/beer/上的服务正在返回 json 但不是jsonp。在 jsonp 标签中搜索“Unexpected token”,你会发现很多解释,比如这个。json 必须包装在一个javascript 函数中,即回调,但它取决于跨域Web 服务是否支持jsonp。这是由浏览器强制执行的,jQuery 无法克服它。

于 2012-04-28T23:07:51.827 回答
1

API 在设计上不支持 JSONP

不,我们不 [支持 JSONP],因为您必须公开您的 API 密钥。最好的办法是在您身边设置一个中间代理。

推特

请注意,Open Beer Database上的人 确实支持 JSONP。

于 2015-04-11T06:54:18.717 回答