2012-07-09 132 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); 
        } 
     }); 

,看看输出