1

抱歉,如果这是重复的,但我无法在任何相关线程上找到答案。我正在尝试从 api 获取 json 并读取内容,但无法解析。代码如下。我正在使用 jsonp,因为 json 不能跨域工作。

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"
}

任何帮助表示赞赏。

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 回答