【问题标题】:ionic / AngularJS - put method return error 400 (bad request) - [ object Object ]ionic / AngularJS - put 方法返回错误 400(错误请求) - [ object Object ]
【发布时间】:2016-05-31 16:12:27
【问题描述】:

我有一个简单的 Web API 和 ionic/angularJs 客户端,用于 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);
        });
    };

【问题讨论】:

  • 请附上源代码
  • @RubenYeghikyan 我添加了 angularJS 方法
  • 确保您正在解析服务器接受的数据。尝试使用 web 工具或 http 插件来查看标题。尝试修改您的请求并重新发送。
  • @RubenYeghikyan 谢谢,我想说我在纯 angularjs 中测试了我的项目,它会很好并且没有任何错误。但是当我想在 Ionic 项目中使用此代码时,它不起作用。我想知道添加或删除操作如何正常工作,但只有编辑/更新功能有错误!

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


【解决方案1】:

我更改了代码并且它的工作..

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 2015-07-05
      • 1970-01-01
      • 2020-02-16
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多