【问题标题】:Baddata Error when getting date || Spring/AngularJS获取日期时出现错误数据 ||弹簧/AngularJS
【发布时间】:2018-10-05 12:29:44
【问题描述】:

我是 Spring 新手,在尝试获取当前日期时永久出现 baddata 错误。

代码:

日期:

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

class GetCurrentDate {

GetCurrentDate(){};

    private Date curr = new Date();
    private String strDateFormat = "hh:mm:ss a";
    private DateFormat dateFormat = new SimpleDateFormat(strDateFormat);
    private String formattedDate = dateFormat.format(curr);

    String getCurrentDate(){return formattedDate;}

}

角度:

    $scope.getTime = function(){
    $http.get('/document/date').then(function(data){
        console.log("Data: " + data);
        $scope.time = data;
    });
}

控制器:

    //Get date
    @GetMapping("/document/date")
    public String gDate(){
        GetCurrentDate tempDate = new GetCurrentDate();
        return tempDate.getCurrentDate();
    }

谢谢!

编辑:http://uixdk.com/angular/docs/error/$http/baddata 说我必须使用自己的响应转换器或将有效的 JSON 数据传递给 transformResponse。但我其实不知道怎么做。

【问题讨论】:

    标签: angularjs spring


    【解决方案1】:

    我不知何故解决了这个问题。这是我的解决方案:

        $scope.getTime = function(){
        $http.get('/document/date', {transformResponse: function(response){
                        return JSON.stringify(response);
                        // JSON.stringify() takes a JavaScript object and transforms it 
                           into a JSON string.
                    }
                }).then(function(response){
            $scope.time = response.data;
        }).catch(function(error){console.log(error)});
    
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-06
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      相关资源
      最近更新 更多