お世話になっております。
WEBAPI 初挑戦で、楽天市場API(楽天市場商品検索API)を試してみています。
jQuery の CDN には、公式の 2.0 系を使用しています。
次のようなhtmlを書いて[検索]ボタンをクリックすると(ID 文字列は変えてあります)
html
1<!DOCTYPE html> 2<html> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5<title>タイトル</title> 6<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="[URLを取得したときのコード]" crossorigin="anonymous"></script> 7<script> 8function test() 9{ 10 var keyword = encodeURIComponent("サンプル"); 11 var url = "https://app.rakuten.co.jp/services/api/IchibaItem/Search/20140222?format=json&keyword=" + keyword + "&applicationId=[楽天アプリID]"; 12 alert(url); 13 14$.ajax({ 15 "url": url, 16 "type": "POST", 17 "scriptCharset":"utf-8", 18 "dataType":"json", 19 "timeout":10000 20}).done(function(data, status) { 21 alert(data); 22}).fail(function(data, status) { 23 alert(status); 24 alert(JSON.stringify(data)); 25}); 26} 27</script> 28</head> 29<body> 30<button onclick="test();">検索</button> 31</body> 32</html>
最初に "error"、次に以下のようなアラートが表示され、エラーになったことがわかります。
json
1{"readyState":4, 2 "responseText":"{\"error\":\"wrong_parameter\",\"error_description\":\"response body only accepts well-formed JSON\"}", 3 "responseJSON":{"error":"wrong_parameter","error_description":"response body only accepts well-formed JSON"}, 4 "status":400, 5 "statusText":"Bad Request" 6}
メッセージからすると、レスポンスの JSON フォーマットが綺麗でないのでダメ!(楽天API と jQuery2.0 との組み合わせでは使えない)のような意味合いに読めますが、世の中ではたくさん使われていることでしょうし、私のコードでおかしなところがあるのだと思います。ご指摘下さると幸いです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/19 10:39