jQuery와 PHP를 함께 사용하는 방법을 배우고 있습니다. 이것은 나의 첫 번째 시도이며, 나는 거의 개념을 얻고있는 것처럼 느낍니다. 그러나 내가 언급하지 못한 문제가 있습니다. 나는 다음과 같은 오류가, PHP 스크립트에 JSON 객체를 게시하고 매개 변수 중 하나를 반환 할 때 :JSON을 PHP로 보내고 응답 받기
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-git2.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<style id="jsbin-css"></style>
</head>
<body>
<button onClick="postData();">Submit me!</button>
<script>
function postData() {
var myData = {
'firstName' : 'John',
'lastName' : 'Doe'
};
$.ajax({
type: "POST",
url: "postData.php",
contentType: "application/json",
data: myData,
success: function(msg){
alert(msg);
},
error: function(err) {
alert('error!' + err);
}
});
}
</script>
</body>
</html>
:
된 index.html을 "의 ... 비 개체의 속성을 얻으려고 노력"
postData.php : 배열이 아닌 오브젝트를 반환에 디폴트 (PHP 버전에 따라)
<?php
$input = file_get_contents('php://input');
$jsonData = json_decode($input);
$output = $jsonData->{'firstName'};
echo $output;
?>
서버에 json 문자열을 수신하고 있습니까? JSON 텍스트를 실제로 보내려면 jquery에 말하지 않았고, 단지 보통의 POST 폼 제출을하기 위해서입니다. 'var_dump ($ input)'은 어떻게 생겼는가? –