2012-04-12 2 views
2

jquery를 사용하여 상호 도메인 요청을 시도했습니다. 클라이언트 측의 의지가 보이는 방법이,jsonp 도메인 간 요청 응답이 파싱하는 동안 null을 제공합니다.

<script src="js/jquery-1.6.2.min.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $.ajax({ 
     type: 'GET', 
     url: "http://www.xserver.com/xdomainhandler.php", 
     processData: true, 
     data: { 
      type:"gotohell" 
     }, 
     dataType: "json", 
     success: function (data) { 
      myglob=data; 
      var repo=JSON.parse(myglob); 
      alert(repo.type); 
     }, 
     error : function(XMLHttpRequest, textStatus, errorThrown){ 
      alert('ends up in error state'); 

     } 
    }); 
}); 
</script> 

과 같이 표시됩니다 요청받는 서버 페이지 코드 :

<?php 
header('Access-Control-Allow-Origin: *'); 
header('Expires: ' . gmdate('r', 0)); 
header('Content-type: application/json'); 
session_cache_limiter('nocache'); 

$arr = array ('response'=>'success','comment'=>'test comment here','type'=>$_GET['type']); 
echo json_encode($arr); 
?> 

을하지만을 나는에 아무것도 없어 요청/응답 프로세스를 완료 할 때 'repo'변수. 난 방화를 사용하여 상기 응답을 확인하고 응답 등을 나타낸다

{"response":"success","comment":"test comment here","type":"gotohell"}

또한 난 myglob가 도시 방화의 DOM 패널에 변수

Object { response="success", comment="test comment here", type="gotohell"} 

체크하지만에 myglob를 파싱 할 때 레포, 아무것도 보여주지 않아 .. 내가 어디로 잘못 가고 있는지. 누군가 나를 도울 수 있어요. 감사! 요 그래서

 var repo=JSON.parse(myglob); 

를 방지하고 당신이 jQuery를 이미 응답을 분석 한 것이다 dataType: 'json'을 공급하기 때문에 단지

alert(data.type); 

답변

3

당신은 jQuery를 그것을 구문 분석으로 분석 할 필요가 없습니다 - 구문 분석 다시 오류가 발생합니다. 다음 줄을 제거하십시오.

var repo = JSON.parse(myglob); 
+0

감사합니다. 그래서 우리가 데이터 형을 json으로 설정하면 자동으로 파싱 될 것인가? – VKGS

+2

@Sekar 네, 당신이 dataType을 설정하지 않으면 jQuery가 헤더에서 추측하려고 시도하고 json이라고 추측하면 구문 분석합니다. 따라서 응답을 구문 분석해서는 안됩니다. –

+0

정답입니다. – VKGS

2

를 호출하기위한

관련 문제