gpt4 book ai didi

javascript - location.reload() 不能很好地工作

转载 作者:行者123 更新时间:2023-12-03 10:15:38 27 4
gpt4 key购买 nike

偶尔会出现此问题

点击下页灰色按钮“添加标签”,输入标签名称并提交。 enter image description here enter image description here

提交操作将刷新此页面并在页面中显示此标签,如下所示: enter image description here

提交操作的javascript代码是:

function tagSubmit(){
var tagName = $("#tagName").val();
jQuery.ajax({
type: "POST",
url: "/m/tag/add",
data : {"taskId":taskId,"tagName":tagName},
dataType : "json",
success: function (msg) { //1/0
if (msg == 1){
location.reload();
}else {
alertWarning("添加失败");
}
}
});
}

Controller.java 是:

@Controller
@RequestMapping("/m/tag")
public class TagController extends ControllerBase {

@Autowired
TagService tagService;

@RequestMapping(value = "/add",method = RequestMethod.POST)
@ResponseBody
public int add(@RequestParam(value = "taskId") long taskId ,@RequestParam("tagName")String tagName){


boolean flag = tagService.addTag(tagName.trim(),taskId) ;

return flag?1:0;

}
}

我点击提交按钮后发生错误: enter image description here

网址 http://172.16.1.5:9082/m/rule/unScheduleRule? 必须需要参数“taskId”:

@RequestMapping(value = "unScheduleRule", method = RequestMethod.GET)
public ModelAndView unScheduleRule(@RequestParam(value = "taskId") long taskId, ModelMap modelMap) {
Task task = taskService.getById(taskId);
ModelAndView view = null;

我该如何修复它?

将参数 required = false 添加到下面的代码中?

public ModelAndView unScheduleRule(@RequestParam(value = "taskId",required=false) long taskId, ModelMap modelMap) 

但是如果没有提供taskId,这个页面将无法正常工作!!!

只是偶尔发生,这让我很困惑

最佳答案

您需要添加event.preventDefault()来阻止表单提交,您可以通过AJAX提交。

If this method is called, the default action of the event will not be triggered.

function tagSubmit(e) {
e.preventDefault();

文档:http://api.jquery.com/event.preventdefault/

关于javascript - location.reload() 不能很好地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29889928/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com