我已经找到了很多用ASP、PHP等检索产品信息的例子,但没有一个是在没有任何服务器端代码的纯javascript中。
仅仅使用javascript是可能的吗?有没有可用的包装器类,或者仅仅是一些示例代码?
谢谢。
发布于 2018-09-17 20:49:47
如其他答案中所述,需要向amazon PA-API发出CORS请求。我正在开发一个铬合金插件,我想在那里获取亚马逊的产品并展示它们。所以,我需要使用javascript来做这件事,但我总是得到跨源错误。
可能的解决方案是使用Rob--W开发的CORS-anywhere api
var cors_api_url = 'https://cors-anywhere.herokuapp.com/';
function doCORSRequest(options, printResult) {
var x = new XMLHttpRequest();
x.open(options.method, cors_api_url + options.url);
x.onload = x.onerror = function() {
printResult(
options.method + ' ' + options.url + '\n' +
x.status + ' ' + x.statusText + '\n\n' +
(x.responseText || '')
);
};
x.send(options.data);
}
(function() {
var outputField = document.getElementById('output');
new1();
function new1() {
// e.preventDefault();
doCORSRequest({
method: 'GET',
url: url,
}, function printResult(result) {
//result contains the response
//write your code here
});
};
})();
您可以参考我的答案来获取解决方案代码:https://stackoverflow.com/a/52357243/5743243
发布于 2011-05-04 00:41:39
我不相信amazon提供了他们的web服务的jsonp版本,所以这不可能直接实现。如果你真的想这样做,你必须使用你的一台服务器作为代理。
Here是一个将亚马逊产品web服务包装在json或jsonp层中的库,并提供了示例代码(如果您自己托管它,则只需执行常规的ajax调用,当然不需要jsonp )。
发布于 2015-04-01 03:52:56
加布是对的。你需要通过代理来传递你的请求。
有关如何工作的示例,请参阅:This web API does not have CORS enabled; how can I use it?。
https://stackoverflow.com/questions/5872635
复制相似问题