Ajax with spring mvc always gets error

Every ajax request gets an error. Ajax Function:

function doAjax() {
            var inputText = $("#info").val();
            $.ajax({
                type: 'POST',
                url: 'ajax',
//                data: ({text: inputText}), 
                dataType: 'json',
                data: 'text='+inputText,
                success: function (response) {
                    $("#result_info").text(response);
                }
               error: function (e) {
                    alert('error' + e.responseText);
                }
            });
        }

Java controller

@RequestMapping(value = {"/ajax"}, method = RequestMethod.POST)
public @ResponseBody String showText(@RequestParam String text) {
    System.out.println(text);
    String returnText = "empty";
    if (!text.isEmpty()) {
        returnText = " response: " + text;
    }

    return returnText;
}

Besides this question you could tell what is the difference between in ajax request

data: ({text: inputText}), 
data: 'text='+inputText,
+4
source share
1 answer

In this particular case, it is better to change dataType to html instead of json. I solved it.

0
source

All Articles