如何在ajax调用中接受来自控制器的字符串返回 [英] How to accept string return from controller in ajax call

查看:459
本文介绍了如何在ajax调用中接受来自控制器的字符串返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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

  1. String insertAllStepDetails(@ModelAttribute("formData") AnyClass obj)
  2. String insertAllStepDetails(HttpServletRequest request)
  1. String insertAllStepDetails(@ModelAttribute("formData") AnyClass obj) or
  2. String insertAllStepDetails(HttpServletRequest request)

还要从ajax request

这篇关于如何在ajax调用中接受来自控制器的字符串返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆