Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Ajax Request - Response Issues with Spring  RSS feed

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Link For Reference https://www.mkyong.com/spring-boot/spring-boot-ajax-example/

1. Don't type any name and hit search button - Bad Request - It Comes to Error Function of Ajax - User Can't be Empty - Form Validation. Ajax response is error in this case.

2. Type "Somu" in name field and hit search button - Business Error - Returned with Success Function. Ajax response is success in this case.

3. Assume i have thrown a run time exception in getSearchResultViaAjax() as below

AjaxResponseBody result = new AjaxResponseBody();
       
        if(1==1) {
              throw new RuntimeException();
        }

The same Ajax error block is getting called.

error: function (e) {

            var json = "<h4>Ajax Response Error </h4><pre>"
                + e.responseText + "</pre>";
            $('#feedback').html(json);

            console.log("ERROR pp : ", e);
            $("#btn-search").prop("disabled", false);

        }

So for Bad Request and Any Exception which comes Out side the box comes in error block

Which one is the right approach in the follow ? If both of them is incorrect, help me with the best practice

One
==
1. Throw form validation errors as as Ajax Response is ERROR with failure message to display
2. Throw Business validation errors as Ajax Response is SUCCESS with failure message to display
3. Throw any other exception as Ajax Response is ERROR with failure message to display

OR

Two
===

1. Throw form validation error as Ajax Response is ERROR with failure message to display ('User Field is Empty')
2. Throw Business validation errors as Ajax Response is FAILURE with failure message to display ('User Name doesn't exist')
3. Throw any other exception as Ajax Response is ERROR with failure message to display

Please advise with suggestions or comments.
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!