이 문제를 해결하는 데 어려움이 있습니다. 내가 무엇을 시도하든 상관없이, PHP는 항상 빈 배열을 반환합니다. 여기 내 주요 파일 (index.php를)의 코드입니다 : "링크"PHP 서버의 JSON 데이터 응답이 비어 있습니다.
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$(".ajaxlink").click(function() {
callServer();
return false; //Stop link from redirecting
});
});
var test = { "testName": "testValue" }
var testJSON = JSON.stringify(test);
function updatePage(data) {
document.getElementById("testDiv").innerHTML = data;
}
function callServer() {
$.ajax({
type: "POST",
url: "ajax/server.php",
data: testJSON,
success: function(data) {
updatePage(data);
},
//Upon error, output message containing a little info on what went wrong
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('An Ajax error occured\ntextStatus = ' + textStatus + '\nerrorThrown = ' + errorThrown + '\nstatus = ' + XMLHttpRequest.status);
}
});
}
</script>
<div id="testDiv">Something here</div>
<a href="test1.htm" class="ajaxlink">Link!</a> <br>
은 기본적으로 당신이를 클릭 callServer() 함수를 실행이. 그런 다음 테스트 json 데이터, 즉 { "testName": "testValue"}를 server.php로 보냅니다. Firebug는 json-data가 실제로 server.php로 전송되었다고보고합니다.
내 server.php은 다음과 같습니다
Array
(
)
아약스 함수의 데이터 타입이 정의되지 않은
, 그래서 어떤 출력 서버 :
<?php
print_r($_POST);
?>
이이 (가) testDiv에서 다음을 반환 .php 파일이 뱉어지면 읽을 수 있어야합니다. 필요한 모든 라이브러리 (json, jquery)가 내 문서에도 포함되어 있습니다. 저는 Apache 2.2와 PHP 5.3.1에서 이것을 실행하고 있지만 웹 서버 (수천 개의 웹 사이트를 호스트하는 호스트)에 똑같은 내용을 보여줍니다. 요청 헤더에 사용 된 content-type은 'application/x-www-form-urlencoded; charset = UTF-8 '이 올바르게 작동해야합니다.
감사합니다. 감사합니다. soren
여기에 동의 ... 당신 stringify 필요가 없습니다. JSON으로 데이터를 보내면됩니다. 테스트 변수를 testJSON 대신 데이터로 저장하십시오. – Lindsay
Firebug가 JSON 데이터를 올바르게 보내고 있다고 말합니다. 내 js의 'test'도 객체처럼 작동합니다. –
추가 테스팅이 끝나면 !!!!! 너무 많이 고마워요 –