0

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 ?

1
  • What error you getting . can you share trace ? Commented Dec 19, 2014 at 14:14

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

6 Comments

thanks pravin for your reply i have handler to handle data the in controller i missed while asking question. Now at last i am getting status from a table and i want to show it on jsp div. And i am passing files in form data.
and pravin if i am keeping process data as false then my request it self is not going to controller. I just want to get one status field from table and assign to a String variable in controller,and i want to return that string to success of ajax call.so i can update one div content with status result scuccessfull or not.
one more thing pravin if i keep my controller retrun type as void then its going in success of ajax call. let me know how to get a string from controller to ajax success
For returning string from controller you can user responseBody and return type of method would be string. That is what you have already in your method it should return string in your ajax response
processData (default: true) Type: Boolean By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.