使用Ajax请求(jQuery)在IBM Connections 6.0中关注社区报400错误求助
Let’s walk through the issues in your code and get this working step by step:
1. Typos That Break the API Call
First off, you’ve got a repeated typo: communitities (extra "i") instead of communities. This appears both in your apiSourceIdentifier variable and the source parameter in your URL. IBM’s API is strict about parameter spelling—this alone would trigger a 400 error.
2. Malformed XML Request Body
The Atom XML you’re constructing has invalid <category> tags. You messed up the quote placement and tag closing syntax. Each category term needs proper quote closure, and the tag should end with /> (not /> inside the quote). Also, you’re missing the required Atom namespace on the <entry> tag, which the server needs to parse the request correctly.
3. Missing CSRF Token
IBM Connections requires a CSRF token for AJAX requests to prevent cross-site request forgery. Without this token in your request headers, the server will reject your call (often with a 400 or 403).
Corrected Code
Here’s the fixed version with all these issues addressed, plus better error debugging:
// Fix the typo in source identifier var apiSourceIdentifier = 'communities', apiTypeIdentifier = 'community', elementUuid = '41759ce1-3a88-4464-b912-8e54e7df453a'; // Properly formatted Atom XML with correct namespace and closed tags var entryToPost = [ '<entry xmlns="http://www.w3.org/2005/Atom">', ' <category scheme="http://www.ibm.com/xmlns/prod/sn/source" term="' + apiSourceIdentifier + '" />', ' <category scheme="http://www.ibm.com/xmlns/prod/sn/resource-type" term="' + apiTypeIdentifier + '" />', ' <category scheme="http://www.ibm.com/xmlns/prod/sn/resource-id" term="' + elementUuid + '" />', '</entry>' ].join(''); // Grab the CSRF token from the page's meta tags (standard in IBM Connections) var csrfToken = $('meta[name="ibm-security-token"]').attr('content'); $.ajax({ method: "POST", url: '/communities/follow/atom/resources?source=communities&type=community', // Fixed source parameter cache: false, data: entryToPost, dataType: 'xml', // jQuery expects a shorthand here, not full MIME type contentType: "application/atom+xml", headers: { 'IBM-Security-Token': csrfToken // Add CSRF token to headers } }).done(function() { alert('done!') }).fail(function(xhr, status, error) { // Log detailed errors to console for easier debugging console.log('Error Status:', status); console.log('Error Message:', error); console.log('Server Response:', xhr.responseText); alert('failed'); });
Additional Notes
- Namespace Requirement: The
xmlns="http://www.w3.org/2005/Atom"on the<entry>tag is mandatory—without it, the server won’t recognize the request as valid Atom format. - Debugging Tips: The updated
failcallback logs server responses to your browser’s console. If you still get errors, check this output—IBM’s API usually returns a detailed error message that tells you exactly what’s wrong. - jQuery Version: Your included jQuery 2.1.1 should work fine with Connections 6.0, but if you run into further issues, try using the same jQuery version that’s loaded natively by Connections to avoid conflicts.
内容的提问来源于stack exchange,提问作者SiriSch

