2012-03-11 2 views
2

를 사용하여 서버 측에 JSON을 보냅니다. php에 :내가 HTML 양식을 가지고 있고이 같은 JSON 객체를 만드는 중이라서 아약스

if (window.XMLHttpRequest) { 
    xmlhttp=new XMLHttpRequest(); 
} 
else { 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
//var url = "organizePeople.php?people=" + escape(people.toJSONString()); 
xmlhttp.open("POST","ServerJSON.php?person="+JSobObject,true); 
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
xmlhttp.send(JSobObject); 
xmlhttp.onreadystatechange=function() { 
    if(xmlhttp.readyState==4) { 
     alert(xmlhttp.responseText); 
    } 
} 

나는이

를하고있는 중이 야 내 서버에서 server.php에서 어떤 반응을 얻고 있지 않다

누구든지 내가 뭘 잘못하고있는 걸 도와 줄 수 있습니까? 나는 학습 단계에있다. JS/AJAX를 사용하여 JSON을 준비하고 서버로 보내고 객체 데이터의 응답을 얻고 싶습니다.

미리 감사드립니다.

답변

0

2 생각입니다. 우선, xmlhttp.onreadystatechange assigment를 send() 호출 이상으로 이동 시키면 좋을 것입니다.

두 번째로, 응답을 받으면 불그스넥으로 확인 했습니까? Ajax가 아닌 PHP 페이지를 직접 호출 해 보았습니까?

+0

코드가 xmlhttp.onreadystatechange를 입력하지 않는 기능() { } 섹션 도움이 될 것입니다 –

3

내가 선호하는 바는 jQuery입니다. 자바 스크립트를 기반으로 한 라이브러리 (이처럼)를 더 쉽게 할 수 있습니다.

$.post({ 
    url: "Server.php", 
    data: JSobObject, 
    dataType: "json" 
}).done(function(msg) { 
    alert(msg); 
}); 

jQuery를 다운로드해야 작업 할 수 있습니다. 먼저 PHP에서 rawpost을 확인해야

0

, $_POST 어쩌면 채우기 내가 100 % 모르겠지만, 아마 당신이

$data = json_decode($_POST, true); 
$person = $data['person']; 

이유 같은 당신이되고 싶을 것이다 Automatedly

0

슬래시 다시 게시물 데이터는 전적으로 JSON으로 인코딩 된 텍스트 문자열이므로 데이터를 개별적으로 액세스하기 전에 전체 게시물을 디코딩해야합니다.

0

사용이 코드 = 당신이

<!DOCTYPE html> 
<html> 

<head> 
    <title>Servlet Example</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> 
<script src="js/jquery.serializeJSON.min.js"></script> 



</head> 

<body> 
<div class="jumbotron text-center"> 
<h1>Submit form Data to Database in JSON</h1> 


</div> 
<div class="container"> 
<div class="row"> 
<div class="col-sm-3"></div> 
<div class="col-sm-5"> 
<h3>Enter the Details : </h3> 
<form name="myform" id="myform"> 
<div class="form-group"> 
<label for="fullName">Name:</label> 
<input type="text" name="fullName" class="form-control"> 
</div> 
<div class="form-group"> 
<label for="email">Email:</label> 
<input type="email" name="email" class="form-control"> 
</div> 
<div class="form-group"> 
<label for="subject">Subject:</label> 
<input type="text" name="subject" class="form-control"> 
</div> 
<div class="form-group"> 
<label for="mark">Mark:</label> 
<input type="number" name="mark" class="form-control"> 
</form> 
</div> 


<button type="submit" class="btn btn-success " id="submitform">Submit</button> 




</div> 
<div class="col-sm-3"></div> 
</div> 

<script> 
$(document).ready(function(){ 
$("#submitform").click(function(e) 
{ 
var MyForm = JSON.stringify($("#myform").serializeJSON()); 
console.log(MyForm); 
$.ajax(
{ 
url : "<your url>", 
type: "POST", 
data : MyForm, 

}); 
e.preventDefault(); //STOP default action 

}); 
}); 
</script> 


</body> 
</html> 
관련 문제