如何在ajax调用中接受来自控制器的字符串返回 [英] How to accept string return from controller in ajax call
问题描述
我有一个jsp页面说了这样的话
I have a jsp page say something like this
<div id="result">
Connection not Enabled
<br>
<br>
<button id="enableconnection">Click to Enable connection
</button></div>
一旦我们单击"Connection not Enabled"(未启用连接)按钮,它将执行一些过程,最后它将调用一个js函数,如下所示:
once we click on connection not Enabled button it will do some process and at last it invoke a js function like this:
$.ajax({
url:contextPath +"/submitAllInfo",
type: 'POST',
data: formdata,
async: false,
processData: false,
contentType: false,
success: function (data) {
$("#result").html(data);
},
error: function (){
alert("error has cocured");
},
cache: false
});
控制器逻辑
@RequestMapping(value = "/action", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public @ResponseBody
String insertAllStepDetails(){
// some code
String t ="done";
return t;
}
现在respose无法成功进入js函数的错误块中,如何在ajax调用的成功块中获取此字符串值?
Now respose is going into error block of js function not in success how to get this string value in success block of ajax call ?
推荐答案
url:contextPath +"/submitAllInfo",
此网址与以下操作不匹配
url:contextPath +"/submitAllInfo",
this url is not matching with below action
@RequestMapping(value = "/action", method = RequestMethod.POST)
,并且您还从ajax request
传递data
,但是controller method
String insertAllStepDetails()
中没有什么要处理的,它应该像
and also you passing data
from your ajax request
but there is nothing to handle in controller method
String insertAllStepDetails()
it should be like
-
String insertAllStepDetails(@ModelAttribute("formData") AnyClass obj)
或 -
String insertAllStepDetails(HttpServletRequest request)
String insertAllStepDetails(@ModelAttribute("formData") AnyClass obj)
orString insertAllStepDetails(HttpServletRequest request)
还要从ajax request
这篇关于如何在ajax调用中接受来自控制器的字符串返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!