2013-10-02 2 views
0

안녕하세요, 저는 도메인 간 콜백을 시도하고 있지만 헤더에는 문제가 있습니다. 나는 많은 튜토리얼을 따라 갔지만 나는 그저 작동하지 않는다.크롬 및 파이어 폭스에서 CORS가 작동하지 않습니다.

이 내가 갔을까요 얼마나 멀리입니다 :

<html> 
<head> 
<meta http-equiv="Access-Control-Allow-Origin" content="*"> 
</head> 
<body> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" ></script> 

<script> 

    jQuery.support.cors = true; 

    $(document).ready(function() { 

     //this would be done in a common script file if you are going to 
     //make a lot of these calls 
     $.ajaxSetup({ type: 'POST', dataType: 'json', contentType: 'application/json', data: {} }); 

     $('#btn').click(function() { 
      //call the web service with the button is clicked 
      $.ajax({ url: 'url_external', 
       data: '{ "some_data:data" }', //ensure the data is enclosed in a quote 
          success: function (data) { 
       alert('here...success'); 
      }, 
      complete: function (data) { 
       alert('here'); 
      } 
      }); 

      //return false to avoid a postback 
      return false; 
     }); 

    }); 
</script> 


<div> 
    <button id='btn'>Load It</button> 
    <div id='sayit'></div> 
</div> 
</body> 
</html> 

그리고 IE9에서이에서만 작동합니다. 내가 놓친 게 있니?

<?php 

header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE'); 
header('Access-Control-Allow-Headers', 'Content-Type'); 

이것은 당신의 url_externalCORS Ajax 요청을 할 수 있습니다 : 모질라 파이어 폭스와 구글 크롬에서

+0

문제는 서버가 OPTIONS 요청을 처리하지 못한다는 것입니다. –

+0

그리고 어떻게 처리할까요? –

+1

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS –

답변

0

'url_external' 서버에 HTTP 응답의 모든이 추가 허용되지 않는 옵션 문제와 405 방법이있다 섬기는 사람.

당신은 아마 당신의 도메인에서만 오는에만 CORS 요청을 허용하기 위해 ...

header('Access-Control-Allow-Origin: http://yourdomain.com'); 

... 이것을 조정 미세하게 할 것입니다.

CORS에 대한 최고의 정보 : Using CORS.

관련 문제