2014-09-16 2 views
2

좀 PHP로 한 페이지 응용 프로그램에 대한 AngualrJS을 사용하고있는 CORS (dev에 ENV로 MAMP 프로.) 각도에서 httpAngualrJS 및 PHP

내가 다시 JSON 나에게주는 제 3 자 API를 제공을 요청받을 허용 http 요청.

요청을하면 "No 'Access-Control-Allow-Origin'헤더가 요청 된 리소스에 있습니다."라는 메시지가 나타납니다.

기본적으로 나는 CORS에 8 시간 이상 머물러 있습니다. 브라우저 (Chrome/FF)를 가져 오는 방법이 CORS를 무시하고 http 요청을 전송하는지 잘 모릅니다. 각도 JS에서

:

factory.fetchJson = function (filePath) { 

     return $http.get(filePath) 
      .success(function (data, status) { 
       return data; 
       console.log('Success Fetch JSON', status); 
      }).error(function (data, status, headers, config) { 
       console.log('Error Fetch JSON', status); 
      }); 
} 

지수 CORS 수 있도록 angualrjs Controller.js에 CORS 수 있도록

myApp.config(['$httpProvider', function($httpProvider) { 
    $httpProvider.defaults.useXDomain = true; 
    $httpProvider.defaults.withCredentials = true; 
    delete $httpProvider.defaults.headers.common["X-Requested-With"]; 
    $httpProvider.defaults.headers.common["Accept"] = "application/json"; 
    $httpProvider.defaults.headers.common["Content-Type"] = "application/json"; 

} 
]); 

설정을 설정 여기

는 Controller.js에 요청을 얻을 수있다 .php 헤더 :

<html> 

<?php 

    // Allow from any origin 
    if (isset($_SERVER['HTTP_ORIGIN'])) { 
     header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}"); 
     header('Access-Control-Allow-Credentials: true'); 
     header('Access-Control-Max-Age: 86400'); // cache for 1 day 
    } 

    // Access-Control headers are received during OPTIONS requests 
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { 

     if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) 
      header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); 

     if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) 
      header("Access-Control-Allow-Headers:  {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}"); 

     exit(0); 
    } 

    echo "You have CORS!"; 

    ?> 

JSOP 또는 MIM 프록시를 따르고 있지 않습니다.

도움말!

답변