php+jQuery+Ajax 간단하게 페이지 비동기 리 셋 실현
JQuery Ajax.html 의 코드 는 다음 과 같 습 니 다(비교적 간단 한$.post 를 사용 합 니 다)
<html>
<head>
<meta charset="UTF-8">
<title>JQueryAjax+PHP</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
</head>
<body>
:<input type="text" id="username" name="username" /><br>
:<input type="password" id="password" name="password" /><br>
<button type="button" class="butn">ajax </button><br>
<span class="con"></span>
<script type="text/javascript">
$(document).ready(function(){
$(".butn").click(function(){
var username = $("#username").val();
var password = $("#password").val();
$.post('ajax.php',{name:username,pwd:password},function(data) {
alert(data);
$(".con").html(data);
})
})
})
</script>
</body>
</html>
ajax.php
<?php
echo ' :',$_POST['name'],', :',$_POST['pwd']."<br>";
// ,
echo " ";
?>
비 json 형식 에서 백 엔 드 는 문자열 만 되 돌 릴 수 있 습 니 다.백 엔 드 가 배열 로 돌아 가 려 면 json 형식 을 사용 할 수 있 습 니 다. 예 를 들 어 JQuery Ajax 의 코드 를 다음 과 같이 수정 합 니 다.
<html>
<head>
<meta charset="UTF-8">
<title>JQueryAjax+PHP</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
</head>
<body>
:<input type="text" id="username" name="username" /><br>
:<input type="password" id="password" name="password" /><br>
<button type="button" class="butn">ajax </button><br>
<span class="con"></span>
<script type="text/javascript">
$(document).ready(function(){
$(".butn").click(function(){
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
url: "ajax.php",
type: "POST",
data:{name:username,pwd:password},
dataType: "json",
error: function(){
alert('Error loading XML document');
},
success: function(data,status){// php
alert(status);
alert(data);
$('.con').html(" :"+data[0]+" :"+data[1]);
}
});
})
})
</script>
</body>
</html>
ajax.php
<?php
$name = $_POST['name'];
$pwd = $_POST['pwd'];
$array = array("$name","$pwd");
// ,
echo json_encode($array);//json_encode
?>
실행 효 과 는 다음 과 같 습 니 다:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Laravel - 변환된 유효성 검사 규칙으로 API 요청 제공동적 콘텐츠를 위해 API를 통해 Laravel CMS에 연결하는 모바일 앱(또는 웹사이트) 구축을 고려하십시오. 이제 앱은 CMS에서 번역된 콘텐츠를 받을 것으로 예상되는 다국어 앱이 될 수 있습니다. 일반적으로 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.