programing

AngularJS - $http.데이터를 json으로 게시

iphone6s 2023. 3. 13. 20:15
반응형

AngularJS - $http.데이터를 json으로 게시

angularjs에 대한 자기완성 지시문을 작성 중인데 문제가 좀 있어요

자동 완성 입력이 가능한 양식을 가지고 있습니다.여기에 무언가를 입력하면 변수라는 용어가 JSON으로 전송됩니다.

여기에 이미지 설명 입력

단, 동일한 함수(다른 각도 컨트롤러의 동일한 함수)를 다른 형태로 사용하는 경우 변수가 완벽하게 전송되고 자동완성이 정상적으로 작동합니다.

여기에 이미지 설명 입력

내 각도 함수는 다음과 같습니다.

$scope.getCustomers = function (searchString) {
    return $http.post("/customer/data/autocomplete",
        {term: searchString})
        .then(function (response) {
            return response;
        });
};

어디가 아프십니까?

JSON.stringify()를 사용하여 json을 랩합니다.

var parameter = JSON.stringify({type:"user", username:user_email, password:user_password});
    $http.post(url, parameter).
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        console.log(data);
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });

$http에서 헤더를 명시적으로 설정하는 것을 검토해 주십시오.post (예시의 2가지 버전 중 어느 것이 동작하고 있는지 잘 모르기 때문에 어플리케이션/json을 넣습니다만, 다른 버전인 경우는 application/x-www-form-urlencoded 를 사용할 수 있습니다).

$http.post("/customer/data/autocomplete", {term: searchString}, {headers: {'Content-Type': 'application/json'} })
        .then(function (response) {
            return response;
        });

가장 적절한 방법은 당신을 사용하여 "get" 요청을 할 때 동일한 코드 각도 사용을 사용하는 것입니다.$httpParamSerializer그럼 Jquery를 전혀 사용하지 않고 다음 작업을 간단히 수행할 수 있도록 컨트롤러에 삽입해야 합니다.$http.post(url,$httpParamSerializer({param:val}))

app.controller('ctrl',function($scope,$http,$httpParamSerializer){
  $http.post(url,$httpParamSerializer({param:val,secondParam:secondVal}));
}

언급URL : https://stackoverflow.com/questions/24545072/angularjs-http-post-send-data-as-json

반응형