我向服务器发送 Ajax 请求,但服务器没有得到响应。
PUT 请求:
function editNavigation() {
var flight={
id:idAction.replace('edit',''),
navigation:newNavigation
};
console.log(flight);
var prefix = '/airline/';
$.ajax({
type: 'PUT',
url: prefix +'flights/' + idAction.replace('edit',''),
data: JSON.stringify(flight),
headers:{contentType: "application/json"},
success: function(receive) {
$("#adminTable").empty();
$("#informationP").replaceWith(receive);
$("#hiddenLi").removeAttr('style');
},
error: function() {
alert('Error edited flight');
}
});
}
Controller :
@RequestMapping(value = prefix + "/flights/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String updateFlight(@PathVariable("id") String id, @RequestBody FlightDto flightDto) {
String returnText = "Flight edited successful";
String str1 = flightDto.getNavigation();
logger.info(str1);
String str2 = id;
logger.info(str2);
return returnText;
}
浏览器控制台:
Object {id: "5", navigation: "sdf"}
PUT http://localhost:8080/airline/flights/5 415 ()
为什么我收到错误 415? 为什么我的 Controller 不发送响应?
请您参考如下方法:
415
共振代码是不支持的媒体类型
。
如果您的服务需要
POST
(或任何类型)请求并且用户发送PUT
请求,然后服务器将给出这种响应。如果用户发送错误的
MediaType
,也会出现此类错误。
在您的代码中,我认为您配置了错误的MediaType
,即APPLICATION_JSON_VALUE
。您可以指定MediaType.APPLICATION_JSON
。
我认为这会有所帮助。