【问题标题】:JQuery $.getJSON from PHP file_get_contents on steamcommunity JSON [closed]来自 Steamcommunity JSON 上 PHP file_get_contents 的 JQuery $.getJSON [关闭]
【发布时间】:2017-11-02 18:45:35
【问题描述】:

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

我无法将网址放入我的$.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();
});

有人可以帮我吗?

【问题讨论】:

  • 本网站不允许使用 JSONP,所以我也没有在这篇文章中找到答案:/
  • 如果网站不允许 JSONP,而您无法控制,则必须在 PHP 中执行调用,因为任何合理的浏览器都会阻止您在客户端执行此操作.

标签: php jquery json steam steam-web-api


【解决方案1】:

如果php.iniallow_url_fopen=1(http://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 标头,以便proxy.php 形成对浏览器更友好的格式。如果您使用文件 URI file:///C:/www/test.html 打开 test.html,Access-Control-Allow-Origin: * 标头还应防止 CORS 阻止 ajax 请求。

【讨论】:

  • 它总是说找不到元素我会放一些代码
  • 所以它对我有用,但只在 Google Chrome 上,Firefox 一直告诉我 CORS 问题
  • @Kryze,我在 chrome 和 firefox 上都试过了,firefox 没有抛出任何错误。我得到 CORS 错误的唯一方法是直接使用文件 URI file:///C:/www/test.html 打开 test.html。但是当我通过我的本地网络服务器通过 url http://127.0.0.1/test.html 打开它时它工作正常。
  • @Kryze,我可能已经为您的问题找到了解决方案。尝试将header("Access-Control-Allow-Origin: *"); 添加到proxy.php 中。您可以在更新的代码中看到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 1970-01-01
相关资源
最近更新 更多