user07
user07

Reputation: 670

How to accept string return from controller in ajax call

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>

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

                });

Controller logic

  @RequestMapping(value = "/action", method = RequestMethod.POST)
@ResponseStatus(value = HttpStatus.OK)
public @ResponseBody
String insertAllStepDetails(){
// some code 
 String t ="done";
return t; 

}

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 ?

Upvotes: 0

Views: 2476

Answers (2)

Pravin
Pravin

Reputation: 1147

url:contextPath +"/submitAllInfo", this url is not matching with below action

@RequestMapping(value = "/action", method = RequestMethod.POST)

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) or
  2. String insertAllStepDetails(HttpServletRequest request)

And also remove processData: false, from ajax request

Upvotes: 0

Alex J
Alex J

Reputation: 1019

In your success function you'll want to display this newly called data, by changing:

$("#result").html("connection has been enabled successfully");

To:

$("#result").text(data);

Upvotes: 1

Related Questions