2016년 3월 5일 토요일

angularjs http reqest 소위, ajax


$scope.search = function() {
// $http.get('/url?str='+$scope.var.str).success(function(data) {
// $scope.result = data;
// });

var req = {
  method: 'POST',
  url: '/url',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  data: { str: $scope.var.str }
}

$http(req).then(
  function(response)
  {
    $scope.result = response.data;
  }, 
  function(response){
    console.log(response);
  });
}

이거는

https://docs.angularjs.org/api/ng/service/$http

여기를 보면 알 수 있는 내용인데...

나는 get 방식의 파라미터를 보내는 방식 때문에 고생 많이 했다.

오브젝트로 보낼 수 있다고 생각했기 때문에 그거 찾느라 몇 시간 헤맨듯...

결국 위에 처럼 url에 덧붙여 주면 되기는 한다는...


(다음날 수정)

젠장. 욕이 나온다.

분명 위와 같이 해서 어제 되는 걸 확인했는데.....

어떻게 하다 보니 또 안 되었다.

결국

https://hello-angularjs.appspot.com/angularjs-http-service-ajax-post-json-data-code-example

여기 링크를 따라서 스프링 받아들이는 것과 jackson library 도 maven 에 넣어주고 하니까

정상적으로 동작하게 되었다.

프로그래밍은 삽질의 연속이다.

몇 년 째 프로그래밍을 해왔는데, 무언가 하나 잘못되서 시간을 낭비하는 일이 많이 발생한다.











댓글 없음:

댓글 쓰기