JQuery 프레임 워 크 기반 AJAX 인 스 턴 스 코드

3142 단어 JQueryAJAX 실례
index.html
 
<html>
<head>
<title>jQuery Ajax </title>
</head>
<script src="./js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){// jQueryready , C main
$("#button_login").mousedown(function(){
login(); // ID "button_login" login();
});
});
function login(){ // login();
var username = $("#username").val();//
var password = $("#password").val();//
$.ajax({ // Ajax
type: "post", // post
url : "login.php", // php
dataType:'json',// php JSON
data: 'username='+username+'&password='+password, // php , u p
success: function(json){// php
//alert(json.username+'
'+json.password); // php (json.username) alert
$('#result').html(" :" + json.username + "<br/> :" + json.password); // php result
}
});
//$.post() :
$('#test_post').mousedown(function (){
$.post(
'login.php',
{
username:$('#username').val(),
password:$('#password').val()
},
function (data) //
{
var myjson='';
eval('myjson=' + data + ';');
$('#result').html(" 1:" + myjson.username + "<br/> 1:" + myjson.password);
}
);
});
//$.get() :
$('#test_get').mousedown(function ()
{
$.get(
'login.php',
{
username:$('#username').val(),
password:$('#password').val()
},
function(data) //
{
var myjson='';
eval("myjson=" + data + ";");
$('#result').html(" 2:" + myjson.username + "<br/> 2:" + myjson.password);
}
);
});
}
</script>
<body>
<div id="result" style="background:orange;border:1px solid red;width:300px;height:200px;"></div>
<form id="formtest" action="" method="post">
<p><span> :</span><input type="text" name="username" id="username" /></p>
<p><span> :</span><input type="text" name="password" id="password" /></p>
</form>
<button id="button_login">ajax </button>
<button id="test_post">post </button>
<button id="test_get">get </button>
</body>
</html>
login.php
 
<?php
echo json_encode(array ('username'=>$_REQUEST['username'],'password'=>$_REQUEST['password']));
?>

좋은 웹페이지 즐겨찾기