[英]ionic / AngularJS - put method return error 400 (bad request) - [ object Object ]
我有一个简单的Web API和ionic/angularJs
Client用于CRUD
操作。 当我在服务器上发送数据进行编辑时,会出现错误。
这是发送数据格式和错误的图片:
angularJS更新方法
$scope.save = function (location) {
var obj = {
//locationId: $scope.id,
locationName: location.locationName,
locationDescription: location.locationDescription
};
console.log('output: ' + JSON.stringify(obj));
$http.put(ServerPath + "/" + $scope.id, $scope.id, obj).success(function (data) {
$state.go('tab.home');
}).error(function (data) {
$scope.error = "An error has occured while adding! " + data;
console.log('Error: ' + data);
});
};
我更改了代码及其工作..
$scope.save = function (location) {
var obj = {
locationId: $scope.id,
locationName: location.locationName,
locationDescription: location.locationDescription
};
$http({
method: 'PUT',
url: ServerPath + "/" + $scope.id,
data: JSON.stringify(obj),
headers: {
'Content-Type': 'application/json'
}
}).
success(function (data, status, headers, config) {
$state.go('tab.home');
}).
error(function (data, status, headers, config) {
console.log('Error: ' + status);
});
当我与$ http&$ resource&postman进行演示以发出PUT请求时,我收到以下消息:
ionic.bundle.js:23826 PUT http://localhost:8100/api/users/1?access_token=97d3258a-5a4e-4af4-bd87-3bb9bbb0039f 400 (Bad Request)
经过几天的搜索和许多实验,我发现了由于我的bean类定义如下的原因:
class User implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
一旦删除了构造函数的定义或添加了非参数构造函数,它就会很好地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.