如何使用java脚本或Jquery从表单调用spring控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24432364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call the spring controller method from form using java script or Jquery
提问by user2593318
Hai i tried calling the controller using
海我尝试使用调用控制器
document.forms[0].value = "getSignFaces";
document.forms[0].submit();
But its not calling method in controller
但它不是在控制器中调用方法
@RequestMapping(value=signFaces.do, method=RequestMethod.POST , params ="getSignFaces")
public String getSignFaces(Model model,@ModelAttribute(HBMSWebConstants.MODEL_SIGN_DETAILS) HBMSSessionDataWO sessionData,
@ModelAttribute SignDetailsForm form,HttpServletRequest request,
HttpServletResponse response,@RequestParam String noOfFaces,
I need to send the noOfFaces to this method.
我需要将 noOfFaces 发送到此方法。
Some how it is failling. Please let me know if i am missing any thing
一些它是如何失败的。如果我遗漏任何东西,请告诉我
采纳答案by Arno_Geismar
I think you can try using an ajax call to do the post to the controller.
我认为您可以尝试使用 ajax 调用向控制器发送邮件。
as an example:
举个例子:
var jsonfile= {json:JSON.stringify(contents)};
$.ajax({
type:'POST',
url: "/yourcontrollermapping/signFaces.do
data: jsonfile,
dataType: "json"
});
and then your controller method:
然后你的控制器方法:
@Controller
@RequestMapping("/yourcontrollermapping"
public class YourController(){
@RequestMapping(value = "/signFaces.do, method = RequestMethod.POST)
public void getSignFaces(@RequestParam("json) String json){
//stuff you want to do
}
}
If you wanne do it javascript native you can :
如果你想这样做 javascript native 你可以:
var jsonfile= {json:JSON.stringify(contents)};
var r = new XMLHttpRequest(); r.open("POST", "yourcontrollermapping/signFaces.do", true); r.onreadystatechange = function () { if (r.readyState != 4 || r.status != 200) return; console.log(r.responseText); }; r.send(jsonFile);