发送json数据的Angularjs收到'POST 400 Bad request'

时间:2014-09-20 09:25:15

标签: ajax json angularjs spring-mvc java-ee

我正试图通过角度将json数据从表单发送到控制器但我得到错误请求。不知道这可能是什么。

Angularjs控制器发送功能

$scope.sendaction = function() {

  var dataPost = {
    idTaskAction : $scope.data.action.idTaskAction,
    idTask : $scope.data.action.idTask
  };

  idTaskAction = 2;

  $http({
    url : '../../taskaction/send/',
    method : 'POST',
    data : JSON.stringify(dataPost),
    headers : {
      'Content-Type' : 'application/json'
    }
  }).success(function(data, status) {
    if (data.msg != '') {
      console.log('Success sending action data.msg: ' + data.msg);
    } else {
      console.log('Success sending action data.errr' + data.error);
    }
  }).error(function(data, status, headers) {    
    console.log('@Error sending action status: ' + status);
    console.log('@headers: ' + headers);
    console.log('@failure message: ' + data.msg );
  });
}

**采用json数据的Spring Controller **

@RequestMapping(value = "/taskaction/send", 
        method = RequestMethod.POST, 
        produces = "application/json")
@ResponseBody
public Map<String, Object> sendAction(Principal principal,
        @RequestBody Map<String, Object> data, @PathVariable Long id) {
    logger.info("Task controller /taskaction/send ...");

    String actionname = (String) data.get("actionname");
    String description = (String) data.get("description");
    String duration = (String) data.get("duration");

    System.out.println("*****>>>>>>" + actionname + "|" + description + "|"
            + duration);

    Map<String, Object> rdata = new HashMap<String, Object>();

    TaskAction action = null;

    rdata.put("success", true);

    return rdata;
}

我从chrome检查员请求中收到的是

Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/myTasks/taskaction/send/
Request Method:POST
Status Code:400 Petición incorrecta
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate
Accept-Language:es-ES,es;q=0.8,en;q=0.6
Connection:keep-alive
Content-Length:18
Content-Type:application/json
Cookie:JSESSIONID=70CB35CA8E24E6ED7E7EB44FADB31BDD
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/myTasks/task/upd/8
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Request Payloadview source
{idTaskAction:2}
Response Headersview source
Connection:close
Content-Language:es
Content-Length:990
Content-Type:text/html;charset=utf-8
Date:Sat, 20 Sep 2014 09:00:14 GMT
Server:Apache-Coyote/1.1

请求只有一个参数 {idTaskAction:2} ,在角度控制器dataPost变量中我保存2,idTask和idTaskAction。

0 个答案:

没有答案