正在使用第三方 JS 获取 JSON 数据

Fetching JSON data using JS from a third party

我正在尝试使用某项服务来检查代理。他们有一个简单的 API,他们从中 return JSON。我想在我的服务器上得到这个 JSON。

无论我做什么,我要么收到 CORS 请求问题,要么收到 SyntaxError: missing ; before statement 消息。

这是我的代码:

<h1>Test Page</h1>
<a href="#" onclick="checkProxy()">Test Button</a>


<script>
function checkProxy() {
  console.log("Checking...");
  $.ajax({
    url: 'http://api.ip2proxy.com/?ip=105.159.246.30&key=demo',
    dataType: 'jsonp',
    success: function(data){
      console.log(data);
    }
  });
}

单击我的 'button' 调用函数,return 出现上述语法错误。将数据类型更改为 JSON 会出现 CORS 错误。

奇怪的是,当我使用不同的 URL 时——我在另一个 Whosebug 线程中找到的例子:http://www.locationbox.com.tr/locationbox/services?Key=3430000202000191008400080609030X20201090060050260003069&Cmd=IlList——它记录的数据就很好。

I'd like to get this JSON on my server.

您需要编写 在服务器 上运行的代码才能做到这一点。您的代码失败的原因是您在 运行 浏览器上 Same Origin Policy comes into play: Your page's origin isn't granted access by that API endpoint via CORS。除非他们允许您访问,或者他们提供您可以使用的 JSONP 端点,否则您不能直接从浏览器查询它。