There is an ajax request, after sending which the error constantly gets out

$(document).ready(function() { $('#regForm').submit(function(e) { var frm = $('#regForm'); e.preventDefault(); var data = {} var Form = this; var token = $("meta[name='_csrf']").attr("content"); var header = $("meta[name='_csrf_header']").attr("content"); $(document).ajaxSend(function(e, xhr, options) { xhr.setRequestHeader(header, token); }); $.each(this, function(i, v) { var input = $(v); data[input.attr("path")] = input.val(); delete data["undefined"]; }); $.ajax({ contentType : 'application/json; charset=utf-8', type : frm.attr('method'), url : frm.attr('action'), dataType : 'json', data : JSON.stringify(data), success : function(data) { alert(data); }, error : function(data,status,er) { alert("error: "+data+" status: "+status+" er:"+er); } }); }); }); 

Here is the method that is called

 @RequestMapping(value = "/user/register", method = RequestMethod.POST) public @ResponseBody String createNewUserAccount( @RequestBody RegistrationForm userForm) { try { userService.registerNewUserAccount(userForm); } catch (DuplicateEmailException e) { log.error(e.getLocalizedMessage()); return "Error!"; } return "Success!"; } 

But even after successful completion of the method, it’s such an error

 data = Object {readyState: 4, responseText: "Success!", status: 200, statusText: "OK"}, status = "parsererror", er = SyntaxError: Unexpected token S at Object.parse (native) at jQuery.extend.parseJSON (http://localhost:8080/static/jquery/jquery-1.10.2.js:550:23) at ajaxConvert (http://localhost:8080/static/jquery/jquery-1.10.2.js:8429:19) at done (http://localhost:8080/static/jquery/jquery-1.10.2.js:8185:15) at XMLHttpRequest.jQuery.ajaxTransport.s.send.callback (http://localhost:8080/static/jquery/jquery-1.10.2.js:8778:8) {message: "Unexpected token S"} 
  • one
    Well, the server does not return json, and in sending the request you have for some reason written to wait for the answer json, that's not working - andreymal
  • 3
    @Andriy Dzigar: Replace in dataType json with text , or return json: return '["Success!"]' . In general, with success, it is recommended to return a user object with allowed properties (REST-way). Read . - romeo

0