【问题标题】:Steam Web API: Get CSGO inventory/ CrossDomainRequestSteam Web API:获取 CSGO 库存/CrossDomainRequest
【发布时间】:2025-11-23 09:20:03
【问题描述】:

我在从本地主机向http://steamcommunity.com/profiles/{steamid}/inventory/json/730/2 发出 ajax 请求时遇到问题

问题似乎是他们没有启用 CORS 标头,所以我必须使用 jsonp。由于 GET 请求返回 json,但我的 ajax 函数期待 json-p 我收到错误:

Uncaught SyntaxError: Unexpected token :
2?callback=jQuery22005740937136579305_1452887017109&_=1452887017110:1 

我需要这个资源,但我不确定如何解决这个问题。我环顾四周,但没有找到任何与这个问题特别匹配的东西。有少数网站能够获取特定用户的库存,因此在某些方面它必须是可能的。

我的 Ajax 调用

    $.ajax({
    url: "http://steamcommunity.com/profiles/76561198064153275/inventory/json/730/2",
    type: 'GET',
    dataType: 'jsonp',
    success: function(response) {
        console.log(response);
        if(response.error){
            alert(response.error_text);
        }else {
            console.log("SUCCESS!!");
        }
    }
});

【问题讨论】:

  • 我刚刚尝试添加 ?callback=?我认为 jquery 会做同样的事情,但仍然没有收到 jsonp。我还尝试将请求的内容类型设置为 application/javascript 和 application/jsonp,但没有收到 jsonp。我已经玩过 Steam api 了,但我不确定你是否能够得到这个。

标签: json ajax steam steam-web-api


【解决方案1】:

我想出了一个解决方法!我将 django 用于我的 Web 应用程序,因此我尝试在服务器端进行请求。

我通过pip安装了requests库(库:http://docs.python-requests.org/en/latest/

在我的 django 应用程序中,我创建了一个由我的 AJAX 请求调用的视图

def get_steam_inv(request):
     user_steam_profile = SteamProfile.objects.get(brokerr_user_id = request.user.id)
     r = requests.get("http://steamcommunity.com/profiles/76561198064153275/inventory/json/730/2")
     return JsonResponse(r.json())

那么我对这个视图的ajax请求:

$.ajax({
    url: "/ajax_get_steam_inv/",
    type: 'GET',
        success: function(response) {
            console.log(response);
            // result = JSON.parse(response);
            if (response.error){
                alert(response.error_text);
            } else {
                console.log(response);
            }
        }
});

现在我有了我需要的数据!

【讨论】: