user3594015 user3594015 -4 years ago 134
jQuery Question

AJAX requests for JSON/JSONP to Disqus API getting 400 response

I am trying to retrieve recent comments on my site/forum via the Disqus API. My request works when I try it in the address bar, but I get 400 errors when I try to make AJAX calls with JQuery.

My JSON request:

$.ajax({
type: 'GET',
url: "https://disqus.com/api/3.0/forums/listPosts.json",
data: { api_key: 'my_public_key', forum : 'my_forum_name'},
cache: false,
dataType: 'json',
success: function (result) {
console.log("SUCCESS");
}
});


When I try the above JSON request, I get an error message in the console that indicates that the Access-Control-Allow-Origin header isn't set in the server response. To me, that suggests that Disqus isn't set up to support CORS requests, but the documentation indicates that Disqus supports JSON as a request type.

My JSONP request:

$.ajax({
type: 'GET',
url: "https://disqus.com/api/3.0/forums/listPosts.jsonp",
data: { api_key: 'my_public_key', forum : 'my_forum_name'},
cache: false,
dataType: 'jsonp',
success: function (result) {
console.log("SUCCESS");
}
});


When I try the above JSONP request per this tutorial provided by Disqus, I still get 400 Bad Request as a response.

When I make both requests with curl using the same development server, I can see the correct JSON or JSONP-formatted response in the response body, though the header lists a 400 error. (However, the Chrome network tab states "this request has no response data available" when I highlight the request there -- maybe I need to be on the dev channel of Chrome to see the response.)

I've tried both requests on a couple of different domains and ensured each time that the domains I'm testing the requests with are listed with Disqus as trusted domains, though I'm not sure that matters.

I'm aware that a proxy would probably fix the JSON problem, but as Disqus states that they support JSON and JSONP requests, I would prefer not to have to do that. What else can I try?

Answer Source

There is a field in Disqus for specifying which URLs can use your Disqus public key (disqus.com/api/applications -> Click on your application -> Navigate to settings tab) that's distinct from the trusted domains field that's in the advanced settings of the admin panel of Disqus. Adding my site's URL to the former fixed the problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download