如何在JIRA插件中使用AJS调用Servlet的POST请求?
Using AJS to Send POST Requests to a Jira Servlet
Absolutely! You can definitely use AJS (Atlassian's jQuery-based utility) to send POST requests to your Jira servlet endpoint—since you already know how to use AJS for REST APIs, the pattern will feel familiar, you just need to tweak it to fit your servlet's needs.
Basic POST Request Implementation
Here's a simple, working example to send a POST request to your servlet URL:
AJS.post({ url: "/jira/plugins/servlet/{servletURL}", // Replace {servletURL} with your actual servlet path data: { // Add your POST parameters here (key-value pairs your servlet expects) userInput: "sample-data", requestType: "create" }, success: function(response) { // Handle successful response from the servlet console.log("POST request completed successfully:", response); // Add your UI updates or follow-up logic here (like showing a success message) AJS.messages.success("#request-status", { title: "Success", body: "Your request was processed correctly." }); }, error: function(xhr, status, error) { // Handle errors (network issues, servlet errors, etc.) console.error("POST request failed:", status, error); // Show user-friendly error message AJS.messages.error("#request-status", { title: "Error", body: "Failed to process your request. Please try again later." }); } });
Important Things to Remember
- Servlet Setup: Double-check that your servlet is configured to handle
POSTrequests—make sure itsdoPost()method is properly implemented (not justdoGet()). - CSRF Protection: No need to worry about CSRF tokens manually; AJS automatically includes the required token in request headers when using
AJS.post(), which complies with Jira's security rules. - JSON Data Format: If your servlet expects JSON instead of form-encoded parameters, adjust the request with a
contentTypesetting:AJS.post({ url: "/jira/plugins/servlet/{servletURL}", contentType: "application/json", data: JSON.stringify({ key1: "value1", key2: "value2" }), success: function(response) { /* ... */ }, error: function(xhr, status, error) { /* ... */ } }); - Full URL Construction: If you need an absolute URL instead of a relative one, use
AJS.contextPath()to get Jira's base path:const fullServletUrl = AJS.contextPath() + "/plugins/servlet/{servletURL}";
Testing Tips
Use your browser's Developer Tools (Network tab) to verify:
- The request method is indeed
POST - The correct parameters/JSON are being sent
- The servlet returns a valid response code (like 200 OK for success)
内容的提问来源于stack exchange,提问作者Imamudin Naseem
相关产品推荐
相关产品推荐

