Frank
Frank

Reputation: 655

location.reload() doesn't work well

this issue occasionally occurred

Clicked the grey button "添加标签" in below page, inputed the tag name and submit it. enter image description here enter image description here

The submit action will refresh this page and show this tag in the page as below: enter image description here

the javascript code of submit action is :

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("添加失败");
        }
    }
});
}

the Controller.java is :

@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;

}
}

the ERROR occurred after I clicked the submit button : enter image description here

The url http://172.16.1.5:9082/m/rule/unScheduleRule? must require the parameter "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;

How can I fix it ?

add the param required = false into the below code ?

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

But if the taskId is not offered ,this page won't work well !!!

It just occasionally occurred ,this makes me very confused

Upvotes: 0

Views: 125

Answers (1)

Tushar
Tushar

Reputation: 87233

You need to add event.preventDefault() to stop form from submitting and you can submit it by AJAX.

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

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

Docs: http://api.jquery.com/event.preventdefault/

Upvotes: 1

Related Questions