2016-07-29 34 views
1

我有角和控制器的$ HTTP功能的数据,但控制器不检索数据从角发送,在日志打印

跳过CORS处理:响应已包含 “访问控制允许来源”头

我有我的configurate滤波器web.xml文件头

response.setHeader("Access-Control-Allow-Origin", "*"); 
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); 
response.setHeader("Access-Control-Max-Age", "3600"); 
response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with"); 

我的角度功能是本

 app.controller('LoadCardController', function($scope, $http) { 

    $scope.executor = function() { 

     var person = { 
      name : 'Prueba', 
      todayDate : 1469679969827 
     }; 

     console.log("DATA=> ", data); 

     $http({ 
      method : 'POST', 
      url : 'http://localhost/myWeb/addPerson', 
      data : person, 
      headers : { 
       'Content-Type' : 'application/json' 
      } 
     }).then(function successCallback(response) { 
      console.log("success"); 
      console.log(response); 
     }, function errorCallback(response) { 
      console.log("error"); 
      console.log(response); 

     }); 
    } 

}); 

和我的控制器是这样的......

@ResponseBody 
      @RequestMapping(path = "/addPerson", method = RequestMethod.POST) 
      public Person addPerson(final Person person) throws CustomException { 

       person.setAge("18"); 
       person.setBithDay("15"); 

       LOGGER.trace("Add person with data {}", person); 

       return person; 

      } 

      }); 
     } 

进来控制器时,人数据为空,对于名称和属性todayDate,由发我角。

我现在控制器是非常简单的,因为只有回到同一个人OBJET

+0

我不认为这与它有什么关系正如你所说,作为Spring控制器的CORS能够接收请求。您是否需要为所有主机启用CORS? – Matt

回答

0

从你这里显示什么,好像请求经历,但数据没有把它到你person Spring控制器方法中的参数。可能有几个不同的原因。

首先,它可能是请求主体没有被映射到您的Spring控制器方法的person参数。您可以通过在方法参数之前使用@RequestBody注释(详细信息here)来告诉Spring自动执行此操作。在您的例子:

@ResponseBody 
@RequestMapping(path = "/addPerson", method = RequestMethod.POST) 
public Person addPerson(@RequestBody Person person) throws CustomException { 

    person.setAge("18"); 
    person.setBithDay("15"); 

    LOGGER.trace("Add person with data {}", person); 

    return person; 

} 

如果不解决这个问题,它可能是与Person类的问题。至少,它需要有:

public class Person { 
    private String name; 
    private String age; 
    private String bithday; 
    private Date todayDate; 

    public Person() { 
     // this depends how your serialization is set up 
    } 

    // getters and setters... 
} 

一种惯例是使用单独的数据传输对象类(即PersonDto),它从该请求体取数据并生成一个Person对象与它(反之亦然),以便您可以控制提交如何映射到后端。有关该模式的更多信息here

0

我会给你一个选择

你的DTO是

public class Person { 
    private String name; 
    private String todayDate; 
    private String age; 
    private String bithDay; 
// getter and setters, etc.... 

AngularJS使用:

// Every properties matches with your DTO, example: name, todayDate looks... 
var formData = new FormData(); 
formData.append("name", 'Prueba'); 
formData.append("todayDate" , '1469679969827'); 

console.log("DATA=> ", formData); 
$http({ 
     method : 'POST', 
      url : 'http://localhost/myWeb/addPerson', 
      data : formData, 
      headers : { 
       'Content-Type' : undefined 
      } 
     }).then(function successCallback(response) { 
      console.log("success"); 
      console.log(JSON.stringify(response)); 
     }, function errorCallback(response) { 
      console.log("error"); 
      console.log(response); 

     }); 

你登录你的浏览器在我的情况下,Chrome会....

success 
myjs.js:1744 {"data":{"name":"Prueba","todayDate":"1469679969827","age":"18","bithDay":"15"},"status":200