无法通过 JQuery 获得 JSON

Can't get JSON with JQuery

我正在尝试从 url 获取 json 但它不起作用,我也没有收到任何错误

index.js:

$(function () {
    $.getJSON("http://telegram-socks.tk/json", function (data) {
            $("textarea").html(JSON.stringify(data));
        }
    );
});

index.html:

<html>
<head>
    <meta name="viewport" content="initial-scale=1" />
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="index.js"></script>
</head>
<body>
    <div class="container-fluid">
        <div class="row text-center">
            <div class="col">
                <textarea class="form-control text-center" rows="50"></textarea>
            </div>
        </div>
    </div>
</body>
</html>

电报袜子。tk/json:

{
    "Proxies": [
        "188.166.91.133:1080", 
        "51.15.100.63:1080", 
        ...
    ]
}

(telegram-socks.tk/json 根据 jsonlint.com 有效 JSON)

试试这个:

$.getJSON("http://telegram-socks.tk/json", function(data) {
      console.log( "success" );
}).fail(function( jqxhr, textStatus, error ) {
console.log(data);
}); 

如果您打开控制台,您可能会看到如下错误:

Mixed Content: The page at 'https://yourpage.com' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://telegram-socks.tk/json'. This request has been blocked; the content must be served over HTTPS.

发生这种情况是因为您在一个通过 SSL 加密的页面上,并试图访问一个不安全的 url,大多数浏览器会在本机阻止。


甚至像这样:

Failed to load http://telegram-socks.tk/json: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

那是因为其他页面的资源不安全,浏览器再次阻止了你的请求

查看下面的代码片段并检查控制台:

$(function() {
  $.getJSON("http://telegram-socks.tk/json", function(data) {
    console.log(data);
  }).error(function(e) {
    console.log(e);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="row text-center">
    <div class="col">
      <textarea class="form-control text-center" rows="50"></textarea>
    </div>
  </div>
</div>

您可以在here and here

中查看更多相关信息