Steam Web API: Get CSGO inventory/ CrossDomainRequest - json

I am having issues making the ajax request from my localhost to 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:
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({
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!!");
}
}
});

I have figured out a workaround! I am using django for my web application and so I tried to make the request server side.
I installed the requests library through pip (The library: http://docs.python-requests.org/en/latest/)
In my django app I made a view that would be called by my AJAX request
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({
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 !

Related

RawPostException: You cannot access body after reading from request's data stream

I am trying to get the data from ajax to django post view, I create the cart with items now I need to get the cart to backend
My js code is:
$.ajax({
url: url,
type: "POST",
data: JSON.stringify({'order_data':order}),
contentType: 'application/json; charset=UTF-8',
success: function (data) {
if (data['success']) {
alert(data['success']);
}
},
error: function (request, error, status) {
if (request) {
}
}
});
My View code that I am using in Django view:
if request.method == "POST":
data = json.loads(request.body)
cart = data['order_data']
Then I am getting that error
RawPostException: You cannot access body after reading from request's data stream
your suggestions will be preferred
You can get values from POST via:
order_data = request.POST.get('order_data')
I found the solution for it where I need to do the little change in Django view
order_data = json.loads(request.POST.get('order_data'))
and my work is done

HTTP Request returning empty element

I try to get a JSON object from a webservice with
MashupPlatform.http.makeRequest(url, {
method: 'GET',
requestHeaders: {"Accept": "application/json"},
forceProxy: true,
onSuccess: function (response) {
console.log("response: " + JSON.stringify(response));
success(response);
},
onFailure: function (response) {
error(response);
},
onComplete: function () {
complete();
}
});
but in the console every time an empty element ({}) gets logged. If I use curl to request that exact same URL I get the response I need. Is the wirecloud proxy unable to request application/json? In my browsers network analysis I see the request including the correct response, but the success function seems to not get that data.
WireCloud proxy supports application/json without any problem. Although the problem may be caused by other parameters, I think that your problem is related to a bad access to the response data. You should use response.responseText instead of using directly the response object (see this link for more info).

Web service call from .ajax, maxJsonLength error

I'm using .asmx web service and web service method can serialize to json string(3506 record) by using JavaScriptSerializer class and i don't get any error from VisualStudio.
On the other hand my phonegap app using this service and i can consume with that. But my last method contents 3506 records and when i call this method from javascript i get
"error during serialization or deserialization using the json
javascriptserializer"
error.
Should i use paging or smth on web service? If yes can anybody tell me how to do that? Like i said i can use this method from browser. i can get this error when i call on javascript.
$.ajax({
type: "POST",
url: "WEB_SERVICE_URL",
data: "{ dummyParameter:dummy }",
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function() {
app.showLoading();
},
success: function(msg) {
clCard = JSON.parse(msg.d);
alert(clCard.length);
},
complete: function() {
app.hideLoading();
},
error: function(msg) {
app.hideLoading();
alert('Error');
}
});
Please have a look at this. I think helped you.

how to set jsonp callback in retrieving restful web service data via jquery

I have asked a question regarding invalid label on firebug which happens when I try to retrieve data from my restful web service. Most of the answers I received was about setting a callback function. but I can't seem to get the right juice from it. can you show me an exact code on how to do it? or atleast correct the code here:
type: "GET",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
processdata:true,
jsonp: false, jsonpCallback: "success",
url: 'http://localhost:8732/Service1/data/10',
success : function success(data) {
jsonResponse = eval("(" + data + ")");
alert(jsonResponse)
},
error : function(req,status, ex) {
alert(ex);
}
Thanks,
Wow, you've got a lot of unnecessary stuff there. Try this:
$.ajax({
url: 'http://localhost:8732/Service1/data/10',
dataType: 'jsonp',
error: function(req, status, ex) {
// your code here
},
success: function(data) {
//your code here
// Please note: you don't need to 'eval' the json response.
// The 'data' variable will be loaded with the object that the json string represents.
// By the way, don't use 'eval', ever.
}
});
jQuery will take care of the callback on the url. See here: http://api.jquery.com/jQuery.ajax/
UPDATE
Why jsonp? If you're getting this from localhost, why not just json? As discussed in the comments below, your server currently is not capable of a jsonp response, but it can do json.

Cross Domain JSON Request Failing

I am trying to run this code in my browser's console:
$.ajax({
dataType: 'json',
url: 'http://www.web2pdfconvert.com/engine?curl=http://www.nytimes.com&outputmode=json?callback=?',
success: function (data) {
if(data.resultcode == 1) {
console.log(true);
} else {
console.log(false);
}
},
});
However, I am getting a Cross Domain Request Error. when I try to make a simple JSON request then also same error occurs, because JSON request cannot be made on Cross Domains. however, when you go to this URL:
http://www.web2pdfconvert.com/engine?curl=http://www.nytimes.com&outputmode=json
You'll be able to see the JSON data. However, a key point written in the documentation of this websites API says that:
json - all conversion data are returned as JSON object. Also JSONP cross domain communication supported usign jQuery.
Thanks in advance.
Use jsonp instead:
$.ajax({
dataType: 'jsonp',
url: 'http://www.web2pdfconvert.com/engine?curl=http://www.nytimes.com&outputmode=json',
jsonp: "callback",
success: function (data) {
if(data.resultcode == 1) {
console.log(true);
} else {
console.log(false);
}
},
});