JSON 格式的 Jquery.ajax API 请求在 Chrome 中导致“Uncaught SyntaxError: Unexpected token :”

2023-12-23

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

这是我尝试过的两个 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);     
            }
    });             

And:

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

两者都会导致此错误:

未捕获的语法错误:意外的标记:

现在,返回的 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/ http://www.jquery4u.com/json/ajaxjquery-getjson-simple/

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

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

我究竟做错了什么?


我的猜测是服务http://api.playground.brewerydb.com/beer/ http://api.playground.brewerydb.com/beer/正在返回 json 但是NOTjsonp。在 jsonp 标签中搜索“Unexpected token”,您会发现许多解释,例如one https://stackoverflow.com/a/10093781/1211981。 json 必须封装在 javascript 函数中,即回调,但这取决于跨域 Web Service 是否支持 jsonp。这是由浏览器强制执行的,jQuery 无法克服它。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JSON 格式的 Jquery.ajax API 请求在 Chrome 中导致“Uncaught SyntaxError: Unexpected token :” 的相关文章

随机推荐