2010-05-24 7 views
2

PHP 페이지로 전송되는 XMLHttpRequest를 읽을 수 있기를 원합니다. 프로토 타입의 Ajax.Request 함수를 사용하고 있으며 간단한 XML 구조를 보내고 있습니다.PHP로 XMLHttpRequest를받는 방법?

PHP 페이지에서 POST 배열을 인쇄하려고하면 출력이 표시되지 않습니다.

도움을 주시면 감사하겠습니다.

편집 : 아래

<html> 
<head> 

<SCRIPT type="text/javascript" src="prototype.js"></script> 

</head> 
<body> 

<script type="text/javascript"> 

var xml='<?xml version=1.0 encoding=UTF-8?>'; 
xml=xml+'<notification>'; 
xml=xml+'heya there'; 
xml=xml+'</notification>'; 
xml=xml+'</xml>'; 


var myAjax = new Ajax.Request('http://localhost:8080/post2.php',{ 
    contentType: 'text/xml', 
    parameters: xml, 
    method: 'post', 
    onSuccess: function(transport){ alert(transport.status); alert(transport.responseText); } 
}); 

</script> 

</body> 
</html> 

post2.php

Welcome <?php print_r($_POST); ?>!<br /> 
+2

코드를 게시 할 수 있습니까? –

+0

답장을 보내 주셔서 감사합니다. 위의 코드 편집을 참조하십시오. – screenshot345

+1

그래서 당신은 환영합니다 배열()! 결과로? – Joseph

답변

1

당신은 input:// 래퍼 또는 $HTTP_RAW_POST_DATAfopen()을 사용할 수 내 코드입니다.

+1

Col가 언급 한 내용은 다음과 같습니다. http://us2.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data – Joseph

+0

설명서에서 바로 _ _ 경고 이 기능은 PHP 5.6.0에서는 사용 불능으로, PHP 7.0.0에서는 제거되었습니다. "_ –

4

정상적인 요청 사항을 읽는 방식과 정확히 일치합니다.

$_GET['varname']

+1

내가 잘못된 것을하지 않는 한, print_r $ _POST를 수행 할 때 빈 배열 만 남았습니다. 내 아약스 호출에서 내 매개 변수는 ' ... 형식의 xml 문자열이어야합니다. 맞습니까? 귀하의 의견을 보내 주셔서 감사합니다. – screenshot345

+1

'print_r ($ _ GET)'을 시도해 볼 수 있습니다. GET을 통해 어떻게 든 보내지는지보기 위해서입니다. – chris12892

+0

@Adrian처럼 빈 문자열이 있습니다. –

0

사용 $_POST['varname'] "방법 : 포스트"당신이 게시물 본문을 보내려면, 당신은 매개 변수 postBody이 필요합니다. 이렇게 뭔가가 작동 할 수도 있습니다.

var myAjax = new Ajax.Request('http://localhost:8080/post2.php',{ 
    contentType: 'text/xml', 
    postBody: xml, 
    method: 'post', 
    onSuccess: function(transport){ 
     alert(transport.status); alert(transport.responseText); 
    } 
}); 

그런데 왜 콘텐츠 주위에 XML 문서를 만들었습니까? XML arround없이 postBody에 "heya there"라는 메시지 만 보낼 수 있습니다.

편집 : http://www.prototypejs.org/api/ajax/options

3

PHP : PHP : 입력하면

Welcome <?php print(file_get_contents('php://input')); ?>!<br /> 

주와 같은 원시 POST 데이터를 읽을 수 있습니다 // // 입력 여기서 당신은 모든 Ajax.Request로 옵션을 찾을 수 있습니다 enctype = "multipart/form-data"에는 사용할 수 없습니다.

+0

$ rawData = file_get_contents ('php : // input'); //perfECT upvoted :) –

+0

여전히 PHP 7에서 작동합니다. –