views:

103

answers:

3

I'm writing a Google Chrome extension for users to monitor their Stack Overflow account and notify them of new reputation, but I'm having problems with the JSON request to access the rep data. I'm using JQuery as my JSON library.

Using the address: http://stackoverflow.com/users/rep/{userid}/{from-time}/{to-time} where the times are in yyyy-MM-dd format, the request isn't working.

This is my javascript code:

var toTime = new Date();
var fromTime = new Date();
fromTime.setHours(toTime.getHours() - 48);

var address = "http://stackoverflow.com/users/rep/" + getUserId() + "/" + \
              formatDate(fromTime,"yyyy-MM-dd") + "/" + \
              formatDate(toTime,"yyyy-MM-dd");

$.getJSON(address, UpdateRepInfo);

the function UpdateRepInfo isn't being called at all.

I can verify the address is valid when I copy and paste the generated address into Chrome, I download the file containing the correct JSON.

I'm still learning a ton about JSON and JQuery so is there any major errors I've made, because I can't work it out.

EDIT: I should point out I have successfully used the Flair json, so I'm sure that the cross-domain call isn't the issue.

EDIT 2: I posted about this on Meta and received an answer from Jeff Atwood: http://meta.stackoverflow.com/questions/31658/jsonp-not-supported-on-rep-rep-graph-url-for-a-reason. I'm marking this as closed as this is no longer applicable and seems to be impossible due to the server side setup of that json.

A: 

You are correct in that the URL is returning JSON, but it doesn't seem to support a callback function. My guess is that jQuery doesn't realize you are building a plugin and is trying to automatically make a JSONP cross-domain request for you which will fail without a callback. I would try making a $.get request like this:

$.get(address, UpdateRepInfo, 'json');

And see if that works for you.

I haven't built a Chrome plugin, but I am assuming it does not limit "cross-domain" requests since it exists outside a domain infrastructure.

Doug Neiner
Unfortunately this doesn't appear to be working. Even if I replace the UpdateRepInfo with a annon function, that isn't being called.
Alastair Pitts
+2  A: 

Is there a cross domain problem going on here? [not sure about chrome extensions] Call should probably be JSONP, but the api does not offer that option.

epascarello
A: 

I'm not 100% sure but server have to support JSONP calls, isn't it?
Maybe SO simply doen't allow you to get its data remotely this way? Maybe you need some more params in URL?

And i'm not sure its pure JSON you get this way... I try to get it this way:

$.ajax({
    url:'http://stackoverflow.com/users/rep/[my-id]/2009-11-01/2009-12-02/',
    type:'get',
    dataType:'json',
    success: function(j){alert(j);},
    error: function(xhr,t,err){console.log(t);}
})

And get error in console. That is why your success handler doesn't fire. Why you get an error? I really not sure... But i think that this (data i get if i put this url in browser) is not JSON...

[{
    "PostUrl": "1666500/1666816#1666816",
    "PostTitle": "Generating Drop Down List",
    "Rep": 30
},
...
{
    "PostUrl": "1795748/1795879#1795879",
    "PostTitle": "button vs input type=”submit” vs a onclick=”document.formname.submit()”",
    "Rep": 40
}]
NilColor
i'm also getting this error. Having trouble nailing down whats what here.
Alastair Pitts
I'm vote that SF doesn't allow remote access - no jsonp allowed.
NilColor
that sure looks like a json array to me... maybe i need glasses..
Sky Sanders