来自另一个域的json使用jquery

时间:2012-07-09 17:16:34

标签: json api jquery jsonp

很抱歉,如果这是重复,但我找不到任何相关主题的答案。我试图从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"
}

感谢任何帮助。

2 个答案:

答案 0 :(得分: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);
                     }
        });

看看输出

答案 1 :(得分: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');                        
                }
            } 
        });
    });
}