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.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With