Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intelijj IDEA state, that success function in AJAX request in never used.

I have a problem and I have no idea what the reason is. I'm testing ajax requests with this code.

    function sendAJAX() {
            var dataToSend = {};
            dataToSend["username"] = $("#username").val();
            dataToSend["age"] = $("#age").val();
            dataToSend["date"] = $("#date").val();

            $.ajax({
                type : "POST",
                contentType : "application/json",
                url : "dotheajax",
                data : JSON.stringify(dataToSend),
                dataType : "json",
                success : function(response) {
                    $("#typeAjaxHere").html(response);
                }
            });
        }

        $("#form").submit(function (event) {
            event.preventDefault();
            sendAJAX();
        })

<div id="form">
<form id="user_form">
    <label for="username">Name</label>
    <input type="text" name="username" id="username">
    <label for="age">Age</label>
    <input type="text" name="age" id="age">
    <label for="date">Birth date</label>
    <input type="text" name="date" id="date">
    <input type="submit" value="Submit">
</form>


@Controller
public class AjaxControllers {
  @RequestMapping(value = {"dotheajax"}, method = RequestMethod.POST)
  public @ResponseBody String testAjax(@RequestBody HumanDomain humanDomain) {
    System.out.println(humanDomain.getUsername());
    System.out.println(humanDomain.getAge());
    System.out.println(humanDomain.getDate());
    return "Success";
  }
}

public class HumanDomain {

String username;

int age;

String date;

//getters and setters here
}

IntelliJ IDEA marks the success in AJAX as "unused property success" and nothing happens obviously in the "success body". I really don't know why. The request is working fine, in console I get the awaited result. My other similar AJAX function works, but I don't send any JSON data there and it is GET instead of POST. Any advice will be really appreciated. P.S. The error and done are also marked as unused.

like image 394
Dinjvald Avatar asked Nov 04 '15 17:11

Dinjvald


1 Answers

I see the same error but it seems to be superficial, meaning it doesn't affect the functionality.

To make the error go away, I surrounded the function with brackets, making it an array containing one function, and it validates.

Example:

$.ajax({
    type : "POST",
    contentType : "application/json",
    url : "dotheajax",
    data : JSON.stringify(dataToSend),
    dataType : "json",
    success : [
        function(response) {
            $("#typeAjaxHere").html(response);
        }
    ]
});

From JQuery documentation,

"As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn."

like image 71
Megan D Avatar answered Oct 26 '22 18:10

Megan D