Reputation: 35
I created code where pass data to spring rest but don't work correctly see my code below:
@PutMapping("/notification/save/{id}")
public @ResponseBody String update(@PathVariable("id") long id, @RequestBody AccountNotification accountNotification){
String result="ok";
ServiceAccount serviceAccount = new ServiceAccount();
serviceAccount.setId(id);
//accountNotification.setServiceAccount( serviceAccount );
//result =notificationsService.update(id,accountNotification);
JSONObject jObject = new JSONObject();
try
{
JSONArray jArray = new JSONArray();
JSONObject accountNotificationJSON = new JSONObject();
if(result.equals("ok")) {
accountNotificationJSON.put("success", "true");
accountNotificationJSON.put("messages", "Successfully Saved");
}
else {
accountNotificationJSON.put("success", "false");
accountNotificationJSON.put("messages", "NOT Successfully Saved");
}
jArray.put(accountNotificationJSON);
jObject.put("data", jArray);
} catch (JSONException jse) {
logger.error( jse.getMessage());
}
return jObject.toString();
}
my javascrit has:
$("#saveChanges").on('click',function(){
var params1={
proxy:$(proxy).val() ,
port:$(port).val(),
uri:$(uri).val(),
actived:$(actived).val()=="on"?true:false
};
$.ajax({
url: 'notification/save/'+id,
params: params1,
type: 'post',
success:function(response) {
if(response.data[0].success == "true") {
$(".removeMessages").html('<div class="alert alert-success alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+
'<strong> <span class="glyphicon glyphicon-ok-sign"></span> </strong>'+response.data[0].messages+
'</div>');
// close the modal
$("#editMember").modal('hide');
} else {
$(".removeMessages").html('<div class="alert alert-warning alert-dismissible" role="alert">'+
'<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+
'<strong> <span class="glyphicon glyphicon-exclamation-sign"></span> </strong>'+response.data[0].messages+
'</div>');
}
},
error: function(x, e) {
alert(x);
}
});
in this situation ajax code return 400. Any body knows why? thanks if withdraw @RequestBody annotation the rest has been called but the parameter accountNotifications is initialized but without valeus passed.
Upvotes: 2
Views: 370
Reputation: 35
I resolved this mode:
@RequestMapping(value={"/notification/save/{id}"}, method = {RequestMethod.GET, RequestMethod.POST},
produces = "application/json"
)
public @ResponseBody String update(@PathVariable("id") long id, AccountNotification accountNotification){
String result="ok";
JavaScript:
$("#saveChanges").on('click',function(){
var params1={
proxy:$(proxy).val() ,
port:$(port).val(),
uri:$(uri).val(),
actived:$(actived).is(':checked')
};
$.ajax({
url: 'notification/save/'+id,
data: params1,
type: 'post',
success:function(response) {
I replaced into js param with data
Upvotes: 0
Reputation: 2134
there is a put mapping @PutMapping("/notification/save/{id}")
in your Rest code, but in your js called by post method type: 'post',
so it returns 400 Bad Request
, you should use type: put
which is equals to your Rest method.
@PutMapping("/notification/save/{id}")
public @ResponseBody String update(@PathVariable("id") long id,
@RequestParam AccountNotification accountNotification){
.....
}
$.ajax({
url: 'notification/save/'+id,
params: params1,
type: 'put',
success:function(){
}})
Upvotes: 1