對不起,如果這是一個重複,但我無法找到任何相關線程的答案。我試圖從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° 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"
}
任何幫助表示讚賞。
-1'「jQuery的方法是添加一個回調功能,並在最後一個隨機數。「這就是它應該做的。請在提問之前多研究一下。例如,請參閱[jQuery.ajax()](http://api.jquery.com/jQuery.ajax/)文檔。 – merv 2012-07-09 18:08:11