我正在尝试调用一个简单的API,该API将向我返回产品信息。为了简单起见,我试图通过简单的控制台日志记录displayName和SKU编号。到目前为止,我一直无法成功完成此操作。
这是API场所:https://partner.element14.com/io-docs
我正在使用产品搜索方法。
这是呼叫成功后返回的内容:
{
"keywordSearchReturn": {
"numberOfResults": 7949,
"products": [{
"sku": "1360829",
"displayName": "SCHURTER - 0001.1004 - Fuse, Cartridge, Fast Acting, 1 A, 250 V, 5.4mm x 22.5mm, 0.2\" x 0.79\", SP Series",
"productStatus": "STOCKED",
"rohsStatusCode": "YES_YES",
"packSize": 1,
"unitOfMeasure": "EACH",
"id": "pf_UK1_1360829_0",
"image": {
"baseName": "/1360825-40.jpg",
"vrntPath": "farnell/"
},
"datasheets": [{
"type": "T",
"description": "Technical Data Sheet (325.09KB) EN",
"url": "http://www.farnell.com/datasheets/2349118.pdf"
}],
"prices": [{
"to": 499,
"from": 10,
"cost": 0.285
}, {
"to": 999,
"from": 500,
"cost": 0.266
}, {
"to": 4999,
"from": 1000,
"cost": 0.241
}, {
"to": 9999,
"from": 5000,
"cost": 0.237
}, {
"to": 999999999,
"from": 10000,
"cost": 0.232
}],
"inv": 192,
"vendorId": "100047",
"vendorName": "SCHURTER",
"brandName": "SCHURTER",
"translatedManufacturerPartNumber": "0001.1004",
"translatedMinimumOrderQuality": 10,
"attributes": [{
"attributeLabel": "Fuse Size Metric",
"attributeValue": "5.4mm x 22.5mm"
}, {
"attributeLabel": "Fuse Size Imperial",
"attributeValue": "0.2\" x 0.79\""
}, {
"attributeLabel": "Breaking Capacity Current AC",
"attributeUnit": "kA",
"attributeValue": "1.5"
}, {
"attributeLabel": "Blow Characteristic",
"attributeValue": "Fast Acting"
}, {
"attributeLabel": "Voltage Rating VAC",
"attributeUnit": "V",
"attributeValue": "250"
}, {
"attributeLabel": "Product Range",
"attributeValue": "SP Series"
}, {
"attributeLabel": "Fuse Current",
"attributeUnit": "A",
"attributeValue": "1"
}],
"related": {
"containAlternatives": false,
"containcontainRoHSAlternatives": false,
"containAccessories": false,
"containcontainRoHSAccessories": false
},
"stock": {
"level": 192,
"leastLeadTime": 71,
"status": 1,
"shipsFromMultipleWarehouses": true,
"breakdown": [{
"inv": 167,
"region": "Liege",
"lead": 71,
"warehouse": "LG1"
}, {
"inv": 25,
"region": "UK",
"lead": 71,
"warehouse": "UK1"
}, {
"inv": 0,
"region": "UK",
"lead": 71,
"warehouse": "GB1"
}, {
"inv": 0,
"region": "-",
"lead": 71,
"warehouse": "PE1"
}],
"regionalBreakdown": [{
"level": 167,
"leastLeadTime": 71,
"status": 1,
"warehouse": "Liege",
"shipsFromMultipleWarehouses": false
}, {
"level": 25,
"leastLeadTime": 71,
"status": 1,
"warehouse": "UK",
"shipsFromMultipleWarehouses": true
}, {
"level": 0,
"leastLeadTime": 71,
"status": 1,
"warehouse": "-",
"shipsFromMultipleWarehouses": false
}],
"expectedStock": {
"level": "300",
"date": "2020-09-02"
},
"expectedStock": {
"level": "300",
"date": "2020-09-23"
}
},
"countryOfOrigin": "CN",
"comingSoon": false,
"inventoryCode": 5,
"nationalClassCode": null,
"publishingModule": null,
"vatHandlingCode": "SLST",
"releaseStatusCode": 4,
"isSpecialOrder": false,
"isAwaitingRelease": false,
"reeling": false,
"discountReason": 30,
"brandId": "100047",
"commodityClassCode": "037004000",
"orderMultiples": "10",
"packageName": null
}]
}
}
再次,我只是想立即获取displayName和SKU。
效果很好!很奇怪,我粘贴了URL:
http://api.element14.com//catalog/products?term=any%3Afuse&storeInfo.id=uk.farnell.com&resultsSettings.offset=0&resultsSettings.numberOfResults=1&resultsSettings.refinements.filters=&resultsSettings.responseGroup=small&callInfo.omitXmlSchema=false&callInfo.callback=&callInfo.responseDataFormat=json&callinfo.apiKey=3vg7dqhbhu2apjjymk4xhzv3
,我得到所有的JSON。 :)
我在所有“相同网站”,跨网域等上进行了尝试阅读-但显然我找不到正确的组合。我尝试添加正确的cookie,更改它们,等等。什么都没有。
以下是我正在尝试使用的测试代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="js/jquery-1.11.0.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>
</head>
<body>
<script>
Cookies.set('name', 'value', { sameSite: 'none' });
Cookies.set('name', 'value', { secure: 'true; secure' });
// Using the core $.a3vg7dqhbhu2apjjymk4xhzv3jax() method
$.ajax({
// The URL for the request
url: "http://api.element14.com//catalog/products?term=any%3Afuse&storeInfo.id=uk.farnell.com&resultsSettings.offset=0&resultsSettings.numberOfResults=1&resultsSettings.refinements.filters=&resultsSettings.responseGroup=small&callInfo.omitXmlSchema=false&callInfo.callback=&callInfo.responseDataFormat=json&callinfo.apiKey=3vg7dqhbhu2apjjymk4xhzv3",
// Whether this is a POST or GET request
type: "GET",
'Access-Control-Allow-Origin': 'http://127.0.0.1:5500/' ,
// The type of data we expect back
dataType: "jsonp",
crossDomain: 1,
sameSite: none
})
// Code to run if the request succeeds (is done);
// The response is passed to the function
.done(function (json) {
console.log(json.keywordSearchReturn);
})
// Code to run if the request fails; the raw request and
// status codes are passed to the function
.fail(function (xhr, status, errorThrown) {
alert("Sorry, there was a problem!");
console.log("Error: " + errorThrown);
console.log("Status: " + status);
console.dir(xhr);
})
// Code to run regardless of success or failure;
.always(function (xhr, status) {
alert("The request is complete!");
});
</script>
</body>
</html>
感谢您提供的任何帮助。我已经坚持了一段时间了。