2012-07-09 56 views
0

對不起,如果這是一個重複,但我無法找到任何相關線程的答案。我試圖從API獲取JSON並閱讀內容,但無法解析。代碼如下。我使用jsonp,因爲json不能跨域使用。json從另一個域使用jquery

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');       
       } 
      } 
     }); 
    }); 
} 

使用此功能,BBY返回一個錯誤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文件上使用了相同的功能,並且工作順利。

從BBY返回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" 
} 

任何幫助表示讚賞。

+0

-1'「jQuery的方法是添加一個回調功能,並在最後一個隨機數。「這就是它應該做的。請在提問之前多研究一下。例如,請參閱[jQuery.ajax()](http://api.jquery.com/jQuery.ajax/)文檔。 – merv 2012-07-09 18:08:11

回答

0

也許這可以幫助你: 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');       
       } 
      } 
     }); 
    }); 
} 
+0

謝謝,這絕對有幫助!我還必須添加「緩存:真」,並照顧它! – fsiddiq 2012-07-09 17:49:37

+0

@fsiddiq不客氣 – 2012-07-09 17:59:18

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); 
        } 
     }); 

,看看輸出

相關問題