对api.upcitemdb.com端点的请求的CORS错误 [英] CORS error for request to api.upcitemdb.com endpoint

查看:91
本文介绍了对api.upcitemdb.com端点的请求的CORS错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是非常简单的代码,但是以某种方式无法正常工作.我要做的是调用的GET API:

I know this is So simple code but somehow its not working. What I what to do is to call GET API of :

https://api.upcitemdb.com/prod/trial/lookup ?upc = 4011200296908

我想通过使用JavaScript或Jquery来调用此HTML.我已经尝试了很多东西,但是没有用.遵循我已应用但无法正常工作的代码:

I want to call this HTML by using JavaScript or Jquery. I have tried many things but its not working. Following code which I have applied but its not working :

<!DOCTYPE html>
<html>
<body>

<button type="button" onclick="loadDoc()">Request data</button>

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      alert("responseText" + this.responseText);
    } else {
      alert("Error");
    }
  };
  xhttp.open("GET", "https://api.upcitemdb.com/prod/trial/lookup?upc=4011200296908", true);
  xhttp.send();
}
</script>

</body>
</html>

我还尝试通过使用Ajax与Jquery调用相同的API.以下是相同的代码:

I also have tried to call same API with Jquery by using Ajax. Following is code for the same :

<!DOCTYPE html>
<html>
<body>
<header>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</header>
<button type="button" onclick="loadDoc()">Request data</button>

<script>
function loadDoc() {
    $.ajax({
        url:"https://api.upcitemdb.com/prod/trial/lookup?upc=4011200296908",
        success:function(response){
          alert(response);          
        },
       error: function() {
        alert("Error");
        }
    });
}
</script>

</body>
</html>

推荐答案

那是经典之作!

如果打开开发人员"选项卡(f12),可能会看到以下错误:

If you open the developer tab (f12) you may see the following error:

无法加载 https://api.upcitemdb.com/prod/trial/lookup?upc= 4011200296908 : "Access-Control-Allow-Origin"标头具有一个值 ' https://www.upcitemdb.com '

Failed to load https://api.upcitemdb.com/prod/trial/lookup?upc=4011200296908: The 'Access-Control-Allow-Origin' header has a value 'https://www.upcitemdb.com'

这意味着服务器 https://api.upcitemdb.com 不希望您使用他在除 https://www.upcitemdb.com 之外的任何网站上的资源. 如果您不拥有该站点,则无法加载该站点的资源,或者应要求管理员授予您的站点授权. 如果您拥有它,则可以了解有关您的CORS配置的更多信息.

This means that the server https://api.upcitemdb.com don't want you to use his resources on any website but 'https://www.upcitemdb.com'. If you don't own this site you cannot load it's resources or should ask the admin to give your site the authorization. If you own it learn more about you CORS configuration.

这篇关于对api.upcitemdb.com端点的请求的CORS错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆