ionic / AngularJS - put方法返回错误400(错误请求) - [object Object]

时间:2016-01-25 17:34:22

标签: javascript angularjs asp.net-web-api ionic-framework

我有一个简单的Web API和ionic/angularJs客户端用于CRUD操作。当我在服务器上发送数据进行编辑时,我会收到错误。

这是发送数据格式和错误的图片:

Error

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);
        });
    };

2 个答案:

答案 0 :(得分:0)

我改变了代码并且工作了..

$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);
        });

答案 1 :(得分:0)

当我使用$ 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;
    }
}

删除构造函数的定义或添加非参数构造函数后,它就可以正常工作。