1

I want to use following ajax method to call a java function (I use Spring Boot and Hibernate) and read the return value afterwards. The function '/myFunction' is correctly called and the return value is a string and is correctly logged in the java function. But I don't get it, how I can use the return message from the java-method in the success-part afterwards. (notifyUser is a little function(title, message) that shows a notification to an user, this function works correctly) By now, notifyUser just post Error [object Object]

$.ajax({
        type : 'POST',
        contentType : 'application/json',
        url : '/myFunction',
        data : JSON.stringify({
            fooM,                   
            fooO,
            fooS
        }),
        dataType : 'json',
        cache : false,
        timeout : 600000,
        success : function(data) {
            notifyUser('Info', data);
        },
        error : function(e) {
            notifyUser('Error', e);
        }           
    }); 

@RestController
@Secured({ "ROLE_USER", "ROLE_ADMIN" })
public class MyController {

@RequestMapping(value = "/myFunction", method = RequestMethod.POST)  
public String myFunction(@RequestBody MyRequestClass request ) {
String return_string = "Check if return value works " + request.getFooM() + request.getFooO() +request.getFooS();
    log.debug(return_string)
    //Up to this step, everything works
    return return_string;
}
}

What do I have to change, that I'm able to get the return value of the called function '/myFunction' in the success part and send the string to notifyUser? Thank you for your answers. Phil

Phil
  • 9
  • 2
  • Did you check [this](https://stackoverflow.com/questions/7672858/return-only-string-message-from-spring-mvc-3-controller) post ? – Swati Aug 17 '20 at 14:46
  • Yes, I added the controller class for more infos. – Phil Aug 18 '20 at 07:36
  • Got it: Need to add a ".responseText" to the data, so I have now notifyUser('Info', data.responseText); and notifyUser('Error', e.responseText); – Phil Aug 19 '20 at 05:44

0 Answers0