使用JQuery提交表单时POST后立即发起GET请求的问题求助
Hey there, let's break down this weird issue where your dynamic form submission is triggering a POST followed right by a GET request. I've worked with similar stack setups before, so here's what to check and fix:
First, Identify the Root Cause
The most common reason for this sequence is a server-side redirect. When your browser sends a POST and gets a 3xx (redirect) response back, it automatically fires a GET to the redirect target. Let's verify this first:
- Open your browser's DevTools (F12) → go to the Network tab.
- Watch the requests when the page loads. Check the status code of your initial POST request. If it's 301/302/307, that's the culprit.
1. Fix Spring Controller Redirect Behavior
Since your backend uses Spring 2.5, check the Controller method handling the POST request:
- If the method returns something like
new ModelAndView("redirect:/some-path")or uses theredirect:prefix, that's causing the browser to follow with a GET.- If you don't need a redirect: Update the method to return the intended view directly (without
redirect:) or return a JSON response if it's an API endpoint. - If you do need to redirect but want to avoid the GET: Consider using a POST redirect pattern (though this is less common) or switch to an AJAX POST instead of form submission (more on that below).
- If you don't need a redirect: Update the method to return the intended view directly (without
2. Check Apache Proxy Configuration
Your Apache load balancer might be modifying requests or sending redirects unexpectedly:
- Look for
RewriteRuleorProxyPassdirectives in your Apache config that could be altering POST requests. - Check Apache's
access.loganderror.logto see if the proxy layer is initiating any redirects before the request reaches WebLogic.
3. Optimize Your Frontend Code
Your current form submission code works, but there are small tweaks to avoid potential edge cases, plus a better alternative if page navigation isn't required:
Tweak the Dynamic Form Code
function navigateWithForm() { // Clean up syntax for readability var form = $('<form>', { method: 'POST', action: url }); form.append(getInput()); form.appendTo('body').submit(); form.remove(); // Remove the form after submission to avoid duplicates } function getInput() { return $('<input>', { type: 'hidden', name: 'header', value: '<%=emailID%>' }); }
Also, your jQuery version (1.4) is extremely outdated—upgrade to a modern stable version (like 3.x) to avoid unpatched bugs.
Switch to AJAX POST (No Page Redirect)
If you don't need to navigate to a new page after the POST, using AJAX gives you full control and eliminates automatic redirects:
$(document).ready(function() { $.ajax({ url: url, method: 'POST', data: { header: '<%=emailID%>' }, success: function(response) { // Handle successful response here console.log('Request succeeded:', response); }, error: function(xhr, status, error) { // Handle errors console.error('Request failed:', error); } }); });
Final Troubleshooting Steps
- Confirm the POST response status code via DevTools.
- Audit your Spring Controller's return logic for unintended redirects.
- Verify Apache proxy config isn't modifying requests.
- Test with updated jQuery or AJAX to rule out frontend quirks.
内容的提问来源于stack exchange,提问作者ramesh027

