2014-12-01 3 views
0

RESTFUL API doc을 읽고 PUT 방법 중 하나에 request body이 필요합니다.제 경우에 REST 요청을하는 방법은 무엇입니까?

상무부

{ 
    "name" : "Toy", 
    "description" : "Kids toy", 
    "price" : 25 
} 

내 현재 요청 코드와 같은 요청 본문에

PUT /api/v1.2/product/{id} 

을 언급하고있다.

$http.put('/api/v1.2/product/' + ID); 

나는 전화를 걸 Angular http 요청을 사용하려고하지만 요청 본문에 요청을하는 방법을 잘하고 있습니다. 누군가가 그것에 대해 나를 도울 수 있습니까? 고마워요!

+0

doc에는 데이터 전달 방법도 나와 있습니다. https://docs.angularjs.org/api/ng/service/$http#put – sp00m

답변

0

를 참조하십시오 당신은 현명한 당신은 아무것도 다른하지 않을 것을 주에 있습니다. 시도 할 수 있습니다 당신은

JS 코드

var data = { 
    "name" : "Toy", 
    "description" : "Kids toy", 
    "price" : 25 
    }; 

    $scope.save = function() {    
     $http.put('/api/v1.2/product/' + ID, data).success(function (data) { 

     }).error(function (data) { 
      $scope.error = "An Error has occured while Saving customer! " + data; 
      $scope.loading = false; 
     }); 
    }; 

API 코드 나 코드 프로젝트 here로 기사를 작성했습니다

public HttpResponseMessage Put(int ID, Product product) 
    { 

    } 

입니다. 여기에서 $ http get, put, delete의 전체 개념을 얻을 수 있습니다.

는 데이터 감안할 때 (여전히 후드 $ HTTP를 사용하지만)

2

데이터는 두 번째 매개 변수가 put이됩니다. 예를 들어 :

var data = { 
    "name" : "Toy", 
    "description" : "Kids toy", 
    "price" : 25 
}; 

$http.put('/api/v1.2/product/' + ID, data) 
    .success(function(response, status, headers, config){ 
     //do something with response here. 
    }) 
    .error(function(response, status, headers, config){ 
     //handle error here. 
     $scope.error_message = response.error_message; 
    }); 

이 더 많은 정보를 위해, 각도는 약간의 구문을 정의 here

0

$resource 가능성이 더 좋은 API를 제공합니다

var data = { 
    "name" : "Toy", 
    "description" : "Kids toy", 
    "price" : 25 
}; 

설정 자원을

다음
var Product = $resource('/api/v1.2/product/:id', { 'put': { method:'PUT' } }); 

요청을 할 그걸로, 당신은 단지 :

Product.put({ id: ID }, data); 

당신은 너무에 콜백을 통과, 응답 또는 오류를 처리하려면 다음

Product.put({ id: ID }, data, function(data){ }, function(error){ }); 

체크 아웃 문서 : https://docs.angularjs.org/api/ngResource

그것은 약속 꽤 멋진 물건을 할 수있다 (아니 그 $를 http는 할 수 없다. $ resource는 좀 더 멋진 IMHO이다.)

기본적으로 GET, POST 및 DELETE 메소드를 제공한다. PUT은 특수한 경우이므로 위에 명시해야하는 이유가 있습니다.

관련 문제