0

Java 8 / Spring 4 / Maven / jQuery

So I crafted a Spring MVC webapp whose index.jsp uses an ajax call

--- index.jsp snippet ----

function popupRuleDeck_update_submit() {
   var formJsonStr = $('#form_popupRuleDeck_update').serialize();
   $.ajax({
     url: '${pageContext.request.contextPath}/ruleDeck_update',
     type: "post",
     dataType: "json",
     contentType: "application/json; charset=utf-8",
     data: formJsonStr,
     success: function( response) {
         $('#ruleDeckTable').row( this).data( response).draw( false);
         return true;
     },
     error: function( response) {
         console.log( response);
         alert( "ruleDeck update error: " + response.message)
         return false;
     }            
   });
}

to a method in my controller

@RequestMapping( value="/ruleDeck_update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody AjaxResponseBody ruleDeckUpdateHandler( @RequestBody RuleDeck formRuleDeck)
{
    AjaxResponseBody result = new AjaxResponseBody( false, "FAILED");

    RuleDeck ruleDeck = null;

    try {
        ruleDeck = tryThisService.findOneRuleDeck( formRuleDeck.getId());
        ruleDeck.setRuleDeckId( formRuleDeck.getRuleDeckId());
        ruleDeck.setRevision( formRuleDeck.getRevision());
        ruleDeck.setName( formRuleDeck.getName());
        ruleDeck.setDeckType( formRuleDeck.getDeckType());
        ruleDeck.setFileLocation( formRuleDeck.getFileLocation());
        ruleDeck = tryThisService.updateOneRuleDeck( ruleDeck);
        result.setStatus( true);
        result.setMessage("update successful");
    } catch (JsonProcessingException e) {
         result.setMessage( "JPE:" + e.getMessage());
    } catch ( Exception e) {
         result.setMessage( "Exception:" + e.getMessage());
    }

    return result;  //should be implicitly converted into json format and send back to the request.
}     

where

public class AjaxResponseBody {

    @JsonView
    private boolean status;

    @JsonView
    private String message;

    // assorted constructors, getters, setters omitted...
}

I do have Jackson databinding dependency in my pom.xml

I have not explicitly configured any Http/Json/Xml message converters.

I've used this page as a template.

The ajax call sends the JSON to the method, which does find and update an existing object, which gets saved in the database, so far so good. The method executes without errors. But the ajax call always hits the error function and response seems to be undefined, as if the method doesn't actually return a serialized AjaxResponseBody object. Obviously, the goal here is to make sure the success function gets hit when the method throws no errors and to hit the error function and extract the error message when the method throws an error.

Or is there some better pattern I should be using?

TIA,

code_warrior

1 Answer 1

1

Could it be that you are using jQuery >= 3.0 and you should use methods done() instead of success() and fail() instead of error() ?

In jQuery documentation :

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are removed as of jQuery 3.0. You can use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

Sign up to request clarification or add additional context in comments.

2 Comments

That was exactly it! Thank You so much, I didn't have much hair left to pull out. Something one doesn't often think about is the subtleties of versions of all the puzzle pieces that make up an app.
@code_warrior You are welcome, I had the same problem once.

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.