Steam Web API:获取CSGO库存/交叉域名请求

时间:2021-06-11 17:00:03

I am having issues making the ajax request from my localhost to http://steamcommunity.com/profiles/{steamid}/inventory/json/730/2

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

The issue seems to be that they do not have CORS headers enabled, so I have to use jsonp. Since the GET request returns json, but my ajax function is expecting json-p I receive an error:

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

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

I need this resource, but I am unsure how to get around this. I've looked around on SO, but haven't found anything that matches this issue specifically. There are a few sites that are able to obtain a specific user's inventory, so in some respect it has to be possible.

我需要这个资源,但是我不知道如何解决这个问题。我已经查过了,但是还没有发现任何与这个问题相关的东西。有一些站点能够获得特定用户的目录,因此在某些方面它必须是可能的。

My Ajax call

我的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!!");
        }
    }
});

1 个解决方案

#1


1  

I have figured out a workaround! I am using django for my web application and so I tried to make the request server side.

我想出了一个变通办法!我在web应用程序中使用django,所以我尝试在请求服务器端进行处理。

I installed the requests library through pip (The library: http://docs.python-requests.org/en/latest/)

我通过pip安装了请求库(库:http://docs.pythonrequests.org/en/latest/)

In my django app I made a view that would be called by my AJAX request

在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())

Then my ajax request for this view:

然后我的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);
            }
        }
});

And now I have the data I needed !

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

#1


1  

I have figured out a workaround! I am using django for my web application and so I tried to make the request server side.

我想出了一个变通办法!我在web应用程序中使用django,所以我尝试在请求服务器端进行处理。

I installed the requests library through pip (The library: http://docs.python-requests.org/en/latest/)

我通过pip安装了请求库(库:http://docs.pythonrequests.org/en/latest/)

In my django app I made a view that would be called by my AJAX request

在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())

Then my ajax request for this view:

然后我的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);
            }
        }
});

And now I have the data I needed !

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