【问题标题】:Ajax post call should return to html pageAjax post 调用应该返回到 html 页面
【发布时间】:2020-03-02 16:29:17
【问题描述】:

我正在从 ajax 调用一个 POST 方法,并希望它返回包含所有属性的 html 页面。

我的代码:

@RequestMapping(value = "/postMethod", method = {RequestMethod.POST, RequestMethod.GET})
public String getPage( @RequestBody(required=false) Object obj, HttpServletRequest request, Model model) throws Exception {

    request.getSession().setAttribute("sess", cmp);
    model.addAttribute("one", list);
    model.addAttribute("two", list2);
    model.addAttribute("object", new object());
    return "htmlpage";
}

Ajax 调用:

var dataTosend = prepareDataToSend(data);
    $.ajax({
        url : getContextPath()
        + "/app/postMethod",
        type : 'POST',
        data : JSON.stringify(dataTosend),
        contentType : 'application/json',
        success : function(data) {
            console.log(data);
            //return to htmlpage
        },
        error : function() {

        }
    });

Myhtml 示例: 它不是一种形式:

<div id="page">
  <!-- HEADER -->
  <div id="header" class="inside content-wrapper">
<!-- MAIN CONTENT -->
  <div id="main-content">
    <div class="content">
      <!-- accounts -->
      <table>
        <tr>
          <td>
            <label style="margin: 0 0 20px 0">Account</label>
          </td>
          <td> &nbsp; </td>
          <td>
            <select id="Dropdown" onchange="onDropdown()">
              <option th:each="type : ${list}" th:value="${type.Id}" th:text="${type.Name}"></option>
            </select> &nbsp; &nbsp;
          </td>
          </tr>
          </table>
          <!..............more div and other html data...........>
          </div>
</div>
</div>
</div>

如果我对该方法进行 get 调用,它将返回到 html 页面,但是当调用后页面不重定向时,我想重定向到 htmlpage.html 并在 post 中设置所有属性,欢迎任何解决方案。

【问题讨论】:

  • I 页面正在使用 $().html(response) 呈现,但其他功能无法正常工作,因为脚本未加载且 url 也未更改。仍在搜索更多
  • 您到底需要什么?用新属性重新加载整个页面?还是只是改变元素?
  • 使用新属性重新加载整个页面。

标签: java jquery html spring rest


【解决方案1】:

谢谢大家的cmets。 因为我想重新加载整个页面,所以我会在成功时重定向到同一页面,因为它同时是 GET 和 POST:

var dataTosend = prepareDataToSend(data);
    $.ajax({
        url : getContextPath()
        + "/app/postMethod",
        type : 'POST',
        data : JSON.stringify(dataTosend),
        contentType : 'application/json',
        success : function(data) {
            console.log(data);
           window.location.replace(getContextPath()+"/app/postMethod");
        },
        error : function() {

        }
    });

在我的情况下它工作正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 1970-01-01
    • 2022-03-07
    • 2015-06-01
    • 1970-01-01
    • 2021-07-26
    • 2021-01-30
    相关资源
    最近更新 更多