开发者_Go百科SO I am using GROUPON API to grab their deals, I am also using jquery's get to get a json response. This is my jquery
$.get('http://api.groupon.com/v2/deals.json',
{
division_id:'boston',
client_id:'mykey',
},
function(deals){
$('#response').html(deals.soldQuantity);
}, 'json');
After this, I do not get a response. I have checked entering the web request manually and it does work. Am I missing something? thanks
You're trying to access a resource on a remote domain with an XmlHttpRequest, which is by default blocked for security reasons by the Same Origin Policy. You need to use JSONP to get the JSON data in this way...but unfortunately it looks like the API you're hitting doesn't support this.
Your only option may be to proxy the request through your own domain, or something like Yahoo Pipes.
XSS (cross site scripting) issues?
I was able to use your code and got a response fine. The error you are experiencing is related to how you handle the results.
Change:
function(deals){
$('#response').html(deals.soldQuantity);
},
to
function(results){
// Assuming you only want the first deal
$('#response').html(results.deals[0].soldQuantity);
},
Please note, this does not have any error handling if you don't get any results back. I'm not familiar enough with the API to know whether that's feasible or not.
精彩评论