JSON:客户端发送的请求在语法上是不正确的

时间:2015-06-26 08:00:06

标签: javascript java jquery json

我有以下电话

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ServiceCell" forIndexPath:indexPath];

// Configure the cell...
Service * serviceObject;
serviceObject = [servicesArray objectAtIndex:indexPath.row];

//Configuring the images
Image * air = [[Image alloc] initWithLocation:@"icons/air.png" andWithServiceID:21];
Image * pest = [[Image alloc] initWithLocation:@"icons/pest.png" andWithServiceID:18];
Image * carpenter = [[Image alloc] initWithLocation:@"icons/carpenter" andWithServiceID:16];
Image * laundry = [[Image alloc] initWithLocation:@"icons/laundry" andWithServiceID:20];
Image * electrician = [[Image alloc] initWithLocation:@"icons/electrician.png" andWithServiceID:17];
Image * plumber = [[Image alloc] initWithLocation:@"icons/plumber.png" andWithServiceID:14];
Image * appliance = [[Image alloc] initWithLocation:@"icons/appliance.png" andWithServiceID:15];

NSArray * images = [[NSArray alloc] initWithObjects:[UIImage imageNamed:air.location], [UIImage imageNamed:pest.location], [UIImage imageNamed:carpenter.location], [UIImage imageNamed:laundry.location], [UIImage imageNamed:electrician.location], [UIImage imageNamed:plumber.location], [UIImage imageNamed:appliance.location], nil];

if([serviceObject.serviceID isEqualToNumber:[[NSNumber alloc] initWithInt:air.serviceID]]){
    cell.imageView.image = images[0];
}else if([serviceObject.serviceID isEqualToNumber:[[NSNumber alloc] initWithInt:pest.serviceID]]){
    cell.imageView.image = images[1];
}else if([serviceObject.serviceID isEqualToNumber:[[NSNumber alloc] initWithInt:carpenter.serviceID]]){
    cell.imageView.image = images[2];
}else if([serviceObject.serviceID isEqualToNumber:[[NSNumber alloc] initWithInt:laundry.serviceID]]){
    cell.imageView.image = images[3];
}else if([serviceObject.serviceID isEqualToNumber:[[NSNumber alloc] initWithInt:electrician.serviceID]]){
    cell.imageView.image = images[4];
}else if([serviceObject.serviceID isEqualToNumber:[[NSNumber alloc] initWithInt:plumber.serviceID]]){
    cell.imageView.image = images[5];
}else{
    cell.imageView.image = images[6];
}

//Setting the row labels
cell.textLabel.text = serviceObject.serviceName;

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;

这是调用以下Java代码

$.getJSON("/svrBooking/json/getEmployeesByManager",{
            manager: "Paul Walker",
            endDate: endDate, --> "Thu May 28 16:52:25 BST 2015"
            startDate: startDate, --> "Thu Apr 30 16:52:25 BST 2015"
            ajax: 'true'
            }, function (result) {...});

我收到以下错误

@RequestMapping(value="/getEmployeesByManager" , method = RequestMethod.GET)
    public @ResponseBody ReservationsCount
    getEmployeesByManager(Model model,@RequestParam String manager,@RequestParam Date startDate, @RequestParam Date endDate) throws ParseException{...}

回应说

GET 'http://localhost:8080/svrBooking/json/getEmployeesByManager?manager=Paul+Walker&endDate=Thu+May+28+16%3A52%3A25+BST+2015&startDate=Thu+Apr+30+16%3A52%3A25+BST+2015&ajax=true'   400 bad request

这只是在我将Date对象传递给getJSON调用时才开始发生的。如果我将日期更改为字符串一切正常。有谁知道这里发生了什么?

2 个答案:

答案 0 :(得分:1)

看看this问题。
Spring不知道如何解析Date参数。您必须在@DateTimeFormat之后添加@RequestParam注释,以告诉Spring期望哪种格式。例如:

@RequestParam @DateTimeFormat("MMddyyyy") Date startDate

在您的Javascript API调用中,您必须以您指定的格式传递日期。

您还应该能够使用ISO格式(我还没有测试过):

@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date startDate

然后,在您的Javascript API调用中,您应该使用toISOString格式化日期:

$.getJSON("/svrBooking/json/getEmployeesByManager",{
            manager: "Paul Walker",
            endDate: endDate.toISOString(), --> "2015-06-26T08:38:26.175Z"
            startDate: startDate.toISOString(), --> "2015-06-26T08:38:45.304Z"
            ajax: 'true'
            }, function (result) {...});

答案 1 :(得分:0)

尝试使用引号包围数据变量,如下所示:

test2