JQuery $.getJSON 来自 PHP file_get_contents 在 steamcommunity JSON

JQuery $.getJSON from PHP file_get_contents on steamcommunity JSON

我尝试使用 http://steamcommunity.com/inventory/XXXXXXXXXX/753/6 来获取 JSON 项。

我无法将 url 放入我的 $.getJson 中,因为存在 CORS « Access-Control-Allow-Origin » 错误。 如果我不想允许 CORS,我该如何绕过它 我在某处读到可以使用 PHP file_get_contents 重定向 json 文件。

Steam 社区不适用于 JSONP

var exercice = {

modules: {}
};

exercice.modules.ajax = (function () {

return {

    recupererJson: function () {
         initial= $.getJSON('proxy.php',function(data){
        })
            initial.done(function(donnes){
                var i = 0;

                $.each(donnes.descriptions,function(key,value){
                        $('tbody').append("<tr>");
                        $('tbody').append("<td>"+value.name+"</td>");
                        $('tbody').append("<td><img src=http://cdn.steamcommunity.com/economy/image/"+value.icon_url+" alt ="+value.name+"/></td>");
                        $('tbody').append("</tr>");


                    });
                    });
    },
    init: function () {
        exercice.modules.ajax.recupererJson();
    }
}})();

$(document).ready(function () {
exercice.modules.ajax.init();
});

有人可以帮助我吗?

如果php.iniallow_url_fopen=1http://php.net/manual/en/filesystem.configuration.php),应该是默认的,那么你可以制作这样一个php文件:

<?php echo file_get_contents('http://steamcommunity.com/inventory/76561198033103987/753/6');

<?php readfile('http://steamcommunity.com/inventory/76561198033103987/753/6');

否则,如果它被禁用并且您无法访问 php.ini,但您启用了 CURL 扩展,那么您可以这样做:

<?php
$ch = curl_init();
curl_setopt($ch, 'http://steamcommunity.com/inventory/76561198033103987/753/6');
curl_setopt($ch, CURLOPT_HEADER, 0);
echo curl_exec($ch);
curl_close($ch);

http://php.net/manual/en/function.curl-init.php

编辑: 看看你的代码,在我看来这里只有一个问题:

initial= $.getJSON('proxy.php',function(data){

应该是

initial= $.getJSON('http://example.com/proxy.php',function(data){

您应该使用完整的 URL。

EDIT2: 我试过这段代码,对我来说效果很好。

http://127.0.0.1/test.html:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script type="text/javascript">
        var exercice = {
            modules: {}
        };

        exercice.modules.ajax = (function () {

        return {

            recupererJson: function () {
                 initial= $.getJSON('http://127.0.0.1/proxy.php',function(data){
                })
                    initial.done(function(donnes){
                        var i = 0;

                        $.each(donnes.descriptions,function(key,value){
                                $('tbody').append("<tr>");
                                $('tbody').append("<td>"+value.name+"</td>");
                                $('tbody').append("<td><img src=http://cdn.steamcommunity.com/economy/image/"+value.icon_url+" alt ="+value.name+"/></td>");
                                $('tbody').append("</tr>");


                            });
                            });
            },
            init: function () {
                exercice.modules.ajax.recupererJson();
            }
        }})();

        $(document).ready(function () {
            exercice.modules.ajax.init();
        });
    </script>
</head>
<body>
    <table>
        <tbody>
        </tbody>
    </table>
</body>
</html>

http://127.0.0.1/proxy.php:

<?php
header("Content-Type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *");
readfile('http://steamcommunity.com/inventory/76561198033103987/753/6');

我添加了 Content-Type header 以便 proxy.php 形成更多的 browser-friendly。此外,如果您使用文件 URI file:///C:/www/test.html.

打开 test.html,Access-Control-Allow-Origin: * header 应该可以防止 CORS 阻止 ajax 请求