2015-01-23 2 views
0

여기에 내 코드 샘플입니다. 요청을 OPTIONS에 설정하고 요청 페이로드가 설정되지 않을 것입니다.요청 페이로드에서 json으로 데이터를 게시하는 방법은 무엇입니까?

CORS에서 Postman 및 Rest Console과 같은 크롬 플러깅을 사용하고 있으므로이 문제가 있는지 확실하지 않습니다. 내가 요청한 두 가지 도구가 요청 방법 : post, Content-Type : applicaion/json 및 $ scope.data의 데이터가 페이로드를 요청하도록 설정되었는지 확인했습니다. 이유는 무엇입니까?

angular.module('httpExample', []).controller('FetchController', ['$scope', '$http', '$templateCache', 
function($scope, $http, $templateCache) { 
$scope.headers= { 
       'Accept': '*/*', 
       'Content-Type': 'application/json; charset=UTF-8' 
      }; 
      $scope.method = 'POST'; 
      $scope.url = 'http://localhost:56652/reqresp.svc/json/post'; 
      $scope.data={"requests":[{"__type":"AuthenticateAndGetProsties:#LabOra.ProsteModul.ReqResp.Requests", "Authentication":{ "__type":"UserAuthentication:#LabOra.ProsteModul.ReqResp","Username":"xxx","Password":"yyyy" }}]}; 

      $scope.fetch = function() { 
       $scope.code = null; 
       $scope.response = null; 

       $http({method: $scope.method, url: $scope.url,data: JSON.stringify($scope.data),headers:$scope.headers, cache: $templateCache}). 
        success(function(data,status) { 
         $scope.status = status; 
         //$scope.data =x2js.xml_str2json(data); 
         $scope.data =data; 
        }). 
        error(function(status) { 
         $scope.data = "Request failed"; 
         $scope.status = status; 
        }); 
      }; 
     }]); 
})(window.angular); 
+0

교차 출처 POST 및 PUT 요청 (및 다른 경우)의 경우 브라우저는 CORS 가용성을 확인하기 위해 먼저 OPTIONS 요청을 보냅니다. 그 후 POST가 표시되지 않으면 어떤 이유로 실패했습니다. 문제를 디버그하는 데 필요한 모든 것이 개발자 콘솔에서 사용 가능해야합니다. – Phil

+0

이고 CORS를 지원하지 않는 API에서 데이터를 수신 할 수 있지만 브라우저는 해당 데이터에 대한 액세스를 거부하고 대신 ajax 오류를 throw합니다. – charlietfl

+0

예, 실제로 문제가 있었으며 agatha rest service가 cors를 사용하지 못했습니다. 감사 :) –

답변

0

CORS 문제 였고 Agatha rest service가 CORS를 사용할 수 없었습니다. agatha에서 CORS를 사용하도록 설정하려면 global.asax.cs에 다음과 같은 구성을 입력해야합니다.

 // enable CORS 
     response.AddHeader("Access-Control-Allow-Origin", "*"); 
     response.AddHeader("X-Frame-Options", "ALLOW-FROM *"); 

     if (context.Request.HttpMethod == "OPTIONS") 
     { 
      response.AddHeader("Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS"); 
      response.AddHeader("Access-Control-Allow-Headers", "Accept"); 
      response.AddHeader("Access-Control-Allow-Headers", "Authorization,Origin,X-Requested-With,Content-Type"); 
      response.AddHeader("Access-Control-Max-Age", "1728000"); 
      response.End(); 
     } 

Enjoy ... !!!

관련 문제