2016-07-29 31 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