You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用jQuery $.post()向Spring Controller提交请求未触发视图跳转

Why Your ModelAndView Isn't Triggering a View Redirect with $.post?

Ah, I totally get where you're stuck here—this is a super common mix-up between AJAX requests and traditional full-page form submissions. Let me break it down for you:

The Core Issue: $.post is an AJAX Request

When you use $.post() (or any AJAX method), you're sending an asynchronous background request to the server. Unlike a regular form submit (which tells the browser to load the server's response as a new page), AJAX requests are designed to fetch data quietly without reloading or redirecting the current page by default.

Your Spring Controller is correctly processing the request and returning the ModelAndView with the "response" view—but the browser isn't using that response to render a new page because the request was sent via AJAX. Instead, the response content (the HTML of your "response" view) is just sitting in the AJAX response object, waiting for your JavaScript to do something with it.

How to Fix It

You have two main options depending on your use case:

Option 1: Ditch AJAX for a Traditional Form Submit

If you don't need to do any client-side logic before submitting the request, just use a standard HTML form. This will let Spring's ModelAndView work as expected, since the browser will handle the server's response as a full page load:

<form action="/process" method="POST">
  <input type="hidden" name="operation" value="...">
  <input type="hidden" name="sessionId" value="...">
  <input type="text" id="order-id" name="orderId">
  <!-- Add other form fields matching your data parameters here -->
  <button type="submit">Submit</button>
</form>

Option 2: Handle the Redirect in Your AJAX Callback

If you need to keep using AJAX (for example, to validate data before sending), modify your Controller to return a JSON response with the redirect URL or status, then trigger the redirect manually in JavaScript:

First, update your Controller to return JSON instead of ModelAndView:

@PostMapping("/process")
@ResponseBody
public Map<String, String> process(HttpServletRequest httpServletRequest) {
  // ... process your request parameters ...
  
  Map<String, String> response = new HashMap<>();
  response.put("status", "SUCCESS!");
  // Add the URL you want to redirect to (or use the redirectUrl from your request)
  response.put("redirectUrl", "/response");
  return response;
}

Then, update your $.post call to handle the response and trigger the redirect:

$.post({
  url: '/process',
  type: 'POST',
  data: { 
    operation: MyObject.operation(), 
    sessionId: response.session.id, 
    orderId: $('#order-id').val(), 
    orderAmount: $('#order-amount').val(), 
    orderDescription: $('#order-description').val(), 
    redirectUrl: MyObject.redirectUrl() 
  },
  success: function(serverResponse) {
    // Check if the request was successful, then redirect
    if (serverResponse.status === "SUCCESS!") {
      window.location.href = serverResponse.redirectUrl;
      // Alternatively, if you want to render the "response" view content on the current page:
      // $('#your-content-container').html(serverResponse.viewHtml);
    }
  }
});

Quick Recap

  • AJAX requests don't automatically trigger page redirects or view renders—you have to explicitly handle the response in your JavaScript.
  • Traditional form submits let Spring handle the view rendering/redirecting natively, just like you expected.

Pick the approach that fits your workflow, and you'll get the behavior you want!

内容的提问来源于stack exchange,提问作者Paul Erdos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:27:24