1

これが繰り返しである場合は申し訳ありませんが、関連するスレッドのいずれにも答えが見つかりませんでした. API から json を取得して内容を読み取ろうとしていますが、解析に問題があります。コードは以下です。json はクロスドメインで機能しないため、jsonp を使用しています。

function getBbyJson()
{
    link = "http://api.remix.bestbuy.com/v1/products(name=playbook*)?show=sku,name,regularPrice,shortDescription&format=json&apiKey="+apikey
    $(document).ready(function(){
        $.ajax({
            type: "GET",
            url: link,
            dataType: "jsonp", 
            success: function(data){
                for (var i = 0,len = data.products.length; i < len; i++) {
                    var name = data.products[i].name;
                    $('<div class="name" id="item_'+i+'"></div>').html(name).appendTo('#container');                        
                }
            } 
        });
    });
}

この関数を使用すると、by は次のメッセージとともに 400 エラーを返します。

"Couldn't understand '/v1/products(name=playbook*)?show=sku,name,regularPrice,shortDescription&format=json&apiKey=apikey&callback=jQuery17206852765618823469_1341853386681&_=1341853391235'"

jquery メソッドは、最後にコールバック関数と乱数を追加しています。

以下のコードを追加して、コールバック関数を削除できます

jsonp: false
jsonpCallback: ""

しかし、jquery によって生成された乱数を取り除くことはできません。ここから先に進む方法がわかりません。ローカルのjsonファイルで同じ関数を使用しましたが、問題なく機能しました。

ブラウザにリンクを貼り付けた場合の by からの戻り json は以下のとおりです。

{
  "queryTime": "0.005",
  "currentPage": 1,
  "totalPages": 2,
  "partial": false,
  "from": 1,
  "total": 15,
  "to": 10,
  "products": [
    {
      "name": "BlackBerry - Leather Sleeve for BlackBerry PlayBook Tablets - Black",
      "shortDescription": "From our expanded online assortment; designed for use with BlackBerry PlayBook tablets; premium-grade leather material; access to ports; reinforced panels",
      "regularPrice": 49.99,
      "sku": 2638153
    },
    {
      "name": "BlackBerry - PlayBook Tablet with 16GB Memory",
      "shortDescription": "BlackBerry PlayBook Tablet operating system7\" HD capacitive screen with multi-touchWi-Fi16GB memorySupports all POP e-mail services",
      "regularPrice": 199.99,
      "sku": 2265381
    },
    {
      "name": "BlackBerry - PlayBook Tablet with 32GB Memory",
      "shortDescription": "BlackBerry PlayBook Tablet operating system7\" HD capacitive screen with multi-touchWi-Fi32GB memorySupports all POP e-mail services",
      "regularPrice": 249.99,
      "sku": 2387032
    },
    {
      "name": "BlackBerry - PlayBook Tablet with 64GB Memory",
      "shortDescription": "BlackBerry PlayBook Tablet operating system7\" HD capacitive screen with multi-touchWi-Fi64GB storage memorySupports all POP e-mail services",
      "regularPrice": 299.99,
      "sku": 2387041
    },
    {
      "name": "BlackBerry - Rapid Charger for BlackBerry PlayBook",
      "shortDescription": "From our expanded online assortment; compatible with BlackBerry PlayBook tablets; 90&#176; magnetic connector; compact design",
      "regularPrice": 69.99,
      "sku": 2638199
    },
    {
      "name": "BlackBerry - Rapid Charger for BlackBerry PlayBook Tablets - Black",
      "shortDescription": "Compatible with BlackBerry PlayBook tablets; charges PlayBook battery; holds PlayBook upright for viewing; magnetically connects to PlayBook",
      "regularPrice": 69.99,
      "sku": 2496254
    },
    {
      "name": "BlackBerry - Refurbished PlayBook Tablet with 16GB Memory - Black",
      "shortDescription": "RefurbishedBlackBerry PlayBook Tablet operating system7\" HD capacitive screen with multitouchWi-Fi16GB memorySupports all POP e-mail services",
      "regularPrice": 159.99,
      "sku": 4063218
    },
    {
      "name": "BlackBerry - Silicone Skin for BlackBerry PlayBook Tablets - Black",
      "shortDescription": "From our expanded online assortment; compatible with BlackBerry PlayBook tablets; silicone material; play-through design",
      "regularPrice": 29.99,
      "sku": 2638162
    },
    {
      "name": "Hip Street - AC/Car Power Adapter Kit for BlackBerry PlayBook Tablets",
      "shortDescription": "Compatible with BlackBerry PlayBook tablets and other devices with a USB charging cable; LED indicator light; USB charging cable; overcurrent protection",
      "regularPrice": 39.99,
      "sku": 3894198
    },
    {
      "name": "Hip Street - Antifingerprint Screen Protector for BlackBerry PlayBook Tablets - Clear",
      "shortDescription": "Compatible with BlackBerry PlayBook tablets; high-quality optical enhanced film; antiscratch hard coating; residue-free adhesive",
      "regularPrice": 19.99,
      "sku": 3894082
    }
  ],
  "canonicalUrl": "/v1/products(name=\"playbook*\")?show=sku,name,regularPrice,shortDescription&format=json&apiKey=ydpyq9h9cmpmzaakbawv9mzk",
  "totalTime": "0.022"
}

どんな助けでも大歓迎です。

4

2 に答える 2

1

多分これはあなたを助けることができます: http://wiki.asp.net/page.aspx/1734/jquery-cross-domain-ajax-call-using-jsonp/

function getBbyJson()
{
    var link = "http://api.remix.bestbuy.com/v1/products(name=playbook*)";
    $(document).ready(function(){
        $.ajax({
            type: "GET",
            url: link,
            crossDomain: true,
            contentType: "application/json; charset=utf-8",
            data: { show: "sku,name,regularPrice,shortDescription",
                    apiKey:apikey},
            dataType: "jsonp", 
            success: function(data){
                for (var i = 0,len = data.products.length; i < len; i++) {
                    var name = data.products[i].name;
                    $('<div class="name" id="item_'+i+'"></div>').html(name).appendTo('#container');                        
                }
            } 
        });
    });
}
于 2012-07-09T17:29:35.037 に答える
1

コードに追加してみてください:

$.ajax({
            type: "GET",
            url: link,
            dataType: "jsonp", 
            success: function(data){
                for (var i = 0,len = data.products.length; i < len; i++) {
                    var name = data.products[i].name;
                    $('<div class="name" id="item_'+i+'"></div>').html(name).appendTo('#container');                        
                }
            }
            error : function(data){
                      console.log(data);
                     }
        });

そして、どのような出力が表示されるかを確認します

于 2012-07-09T17:29:45.540 に答える