使用 Ajax 放置方法(脚本未触发)



我正在使用Ajax实现Put方法:

<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#submitPut').click(  function () {
console.log("click");
var sendName = $('#name').val();
var sendDescription = $('#description').val();
$.ajax({
url: '/musicalstyle',    //Your api url
type: 'PUT',   //type is any HTTP method
data: {
name: sendName,
description: sendDescription,
},      //Data as js object
success: function () {
}
})
;
});
});
</script>
</head>
<body>
<form> 
<label for="name">Nombre</label>
<input type="text" id="name"  />
<label for="description">Descripción</label>
<input type="text" name="description" id="description"/>
<input id="submitPut" type="button"  value="SubmitPut">
</form>
</body>
</html>

这种形式工作正常,但是当在真实环境中引入它时,不会调用脚本(我在生产环境中的观点是使用 Thymeleaf 和 Boostrap(。就像<button class="btn btn-primary" id="submitPut" type="button">Actualizar</button>没有触发提交放置函数一样。

这里的生产环境形式:

<form>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Nombre</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="name">
</div>
</div>
<div class="form-group row">
<label for="description" class="col-sm-2 col-form-label">Descripción</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="description">
</div>
</div>
<br>
<button class="btn btn-primary" id="submitPut" type="button">Actualizar</button>
</form>

如何强制按钮调用 ajax 函数?

谢谢

在查询时调用 onclick 函数时,输入的 idsubmitsubmitPut所以

<input id="submitPut" type="button"  value="SubmitDelete"/>

@Biplove我已经编辑了我的评论。输入的 ID 等于点击函数的名称。

导入 JQuery 后<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><script>window.jQuery || document.write('<script src="../js/jquery.slim.min.js"></script>')</script><script src="../js/bootstrap.bundle.js"></script></body>

Ajax 被执行。

我的方法放入控制器应该重定向到另一个视图,并且似乎正在重定向。春季日志显示:

2020-05-21 16:04:15.367 DEBUG 552 --- [nio-8182-exec-9] o.s.web.servlet.DispatcherServlet        : PUT "/musicalstyle", parameters={masked}
2020-05-21 16:04:15.368 DEBUG 552 --- [nio-8182-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to com.musicnet.springboot.controller.SystemAdministratorController#updateStyle(Style, Model)
2020-05-21 16:04:15.558 DEBUG 552 --- [nio-8182-exec-9] o.s.w.s.v.ContentNegotiatingViewResolver : Selected '*/*' given [*/*]
2020-05-21 16:04:15.632 DEBUG 552 --- [nio-8182-exec-9] o.s.web.servlet.DispatcherServlet        : Completed 200 OK

此处的样式已更新,因此 put 方法工作正常,但视图未显示在浏览器上。我怀疑 Spring 在重定向视图方面没有任何问题,问题是 ajax 脚本没有重定向到返回的视图。

如何修改 ajax 脚本来处理 Spring 返回的视图?

编辑

事实上,我得到了重定向放入 ajax 脚本:

success: function () {
window.location.reload();
}

最新更新