如何在 $http.get 请求中传递日期

3
我有一个任务列表存储在我的数据库中。我想按日期(完成日期)筛选任务,如何通过$http get请求传递日期。我尝试了一些代码,但是它报错了。能否有人帮助我。
Angular:
$scope.date=1468175400000;
var deadline = new Date($scope.date);
        $http.get(
                '/user/task/gettasks/?status=' + $scope.status
                        + '&priority=' + $scope.priority
                        + '&projectId=' + $scope.project+'&deadline='+deadline).success(
                function(response) {
                    debugger
                    $scope.tasks = response;
                }).error(function(error) {
            console.log(error)
        })

服务(Spring):

@RequestMapping("/gettasks")
    @JsonView({ TaskJsonView.Summary.class })
    public List<Task> getTasks(@RequestParam(value="status" ) String status,
            @RequestParam("priority") String priority,
            @RequestParam("projectId") String projectId,@RequestParam("deadline") Long deadline)  {
System.out.println(deadline);
        return taskControllerService.getTasks(status, priority, projectId,deadline);

    }

错误:

Object {
    timestamp: 1468819101831,
    status: 400,
    error: "Bad Request",
    exception: "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
    message: "Failed to convert value of type [java.lang.String]… "
    MonJul11201600: 00: 00 GMT0530(IndiaStandardTime)
    ""…
}
error: "Bad Request"
exception: "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException"
message: "Failed to convert value of type [java.lang.String] to required type [java.lang.Long]; nested exception is java.lang.NumberFormatException: For input string: "
MonJul11201600: 00: 00 GMT0530(IndiaStandardTime)
""
path: "/user/task/gettasks/"
status: 400 timestamp: 1468819101831 proto: Object

看起来你正在传递一个日期对象作为 deadline。改为传递 deadline.getTime(),这将以长时间戳的形式发送截止日期。 - Cyril Cherian
能给我代码吗? - naresh vadlakonda
2个回答

3

在截止日期部分,您正在传递一个日期对象。改为传递 new Date($scope.date).getTime(),这将以长时间戳形式发送截止日期。

var deadline = new Date($scope.date).getTime();

如果我输入 deadline.getTime(),日期会显示为 1468175400000。 - naresh vadlakonda

0

从异常信息中可以看出,截止日期未被正确解析。在用户界面上,您发送的是日期对象,而在服务器端,则接受长整型值。请确保在两端保持数据类型相同。

如果日期是纪元时间格式,则首先进行转换,并将其附加到请求中。

var time        = new Date(0);
var deadLineDate  = deadLineDate / 1000;
time.setUTCSeconds(deadLineDate);
requestObject = time.toISOString().slice(0, 10);

现在的requestObject是字符串,请在服务器端接受为字符串。

如果有帮助,请告诉我。


你的服务器实现是否与以下代码相同。 @RequestMapping("/gettasks") @JsonView({ TaskJsonView.Summary.class }) public List<Task> getTasks(@RequestParam(value="status" ) String status, @RequestParam("priority") String priority, @RequestParam("projectId") String projectId,@RequestParam("deadline") String deadline) { - thiru_ja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接