2012-04-28 4 views
2

jquery.ajaxでhttp://developer.pintlabs.com/brewerydb/api-documentationを照会しようとしています。JSON形式のJquery.ajax APIリクエストにより、Chromeで「Uncaught SyntaxError:Unexpected token:」が発生する

これらは私が( "O3tmVIは" ダミーIDであることに注意してください)しようとしたjQueryの要求の2つです:

$.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” error can be fixed by passing the JSON data to the js callback

しかし、彼が何を言っているのか分かりません。

私は間違っていますか?

+0

正確にエラーが –

+0

O3tmVIスローされる行示します1UncaughtにSyntaxError:予期しないトークン:03tmVI:1、O3tmVIが返される(1 - ライン)JSONオブジェクト – matthiasdv

+0

何がしなければデータ= $ .parseJSON(データ)であることとを? –

答えて

2

私の推測では、http://api.playground.brewerydb.com/beer/にあるサービスはjsonを返すが、ではなく jsonpです。 jsonpタグで「Unexpected token」を検索すると、oneなどの多くの説明が見つかります。 jsonは、javascript関数、つまりコールバックにラップする必要がありますが、jsonpをサポートするのはクロスドメインWebサービスまでです。これはブラウザによって強制され、jQueryはそれを克服できません。

+0

はい。サーバーはJSONPではなくJSONを返します。それ以外のものを変更してJSONPをサポートするようにリクエストすることはできません。今のところ私はYQLを使ってリクエストを行い、それをJSONPに変換して私に返します – matthiasdv

+0

これは今までに起こったのですか?私は彼らのドキュメンテーションを素早くスキャンし、彼らが行うことを示唆するものは何も見つかりませんでした。 – am17torres

1

APIは設計

no we don't [support JSONP], as you would have to expose your API key. Best thing to do is setup an intermediate proxy on your side.

でJSONPをサポートしていませんtwitterを参照してください。

Open Beer DatabasedoでJSONPをサポートしていますのでご注意ください。