`

第13章 Ajax进阶(下)

 
阅读更多

index.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax</title>
<script type="text/javascript" src="jquery-1.10.1.js"></script>
<script type="text/javascript" src="demo.js"></script>
<link type="text/css" rel="stylesheet" href="style.css" />
</head>
<body>
 <form>
 	用户名:<input type="text" name="user" />
 	邮件:<input type="text" name="email" />
 	<input type="radio" name="sex" value="男" />男
 	<input type="radio" name="sex" value="女" />女
 	<input type="button" value="提交" />
 </form>
 <span class="loading">正在加载中...</span>
 <div id="box"></div>
</body>
</html>

 

style.css

/* CSS Document */
.loading{display: none; color: #c00; font-weight: bold;}

 

user.php

<?php
	echo $_POST['user'].'-'.$_POST['email']
?>

 

test.json

[
	{
		"url":"www.onestopweb.cn"
	}
]

 

jsonp.php

<?php
	$_arr = array('a'=>1,'b'=>2,'c'=>3);
	$_result = json_encode($_arr);
	echo $_result;

?>

 

test1.php

<?php
	echo 'test1.php';
?>

 

test2.php

<?php
	echo 'test2.php';
?>

 

demo.js

$(function(){
	/*
	$('form input[type=button]').click(function(){
		$.ajax({
			type:'POST',
			url:'test.json',
			success:function(response,status,xhr){
				alert(response[0].url);
			}
		});
	});
	
	$('form input[type=button]').click(function(){
		$.ajax({
			type:'POST',
			url:'test.json',
			dataType:'html',
			success:function(response,status,xhr){
				alert(response);
			}
		});
	});
	
	//本地获取 jsonp.php 文件,成功
	$('form input[type=button]').click(function(){
		$.ajax({
			type:'POST',
			url:'jsonp.php',
			dataType:'json',
			success:function(response,status,xhr){
				alert(response);
				alert(response.a);
			}
		});
	});
	
	$('form input[type=button]').click(function(){
		$.ajax({
			type:'POST',
			url:'user.php',
			data:$('form').serialize(),
			//只能显示第二个
			success:function(response,status,xhr){
				alert(response +'1');
			},
			success:function(response,status,xhr){
				alert(response+'2');
			}
		});
	});
	
	//jqXHR 就是 $.ajax() 返回的对象
	$('form input[type=button]').click(function(){
		var jqXHR = $.ajax({
			type:'POST',
			url:'user.php',
			data:$('form').serialize()
		});
		jqXHR.success(function(response){
			alert(response);
		});
	});
	
	//jqXHR 就是 $.ajax() 返回的对象
	$('form input[type=button]').click(function(){
		var jqXHR = $.ajax({
			type:'POST',
			url:'user.php',
			data:$('form').serialize()
		});
//		jqXHR.success(function(response){
//			alert(response);
//		});
		jqXHR.done(function(response){
			alert(response +'1');
		}).done(function(response){
			alert(response +'2');
		});
		
		jqXHR.done(function(response){
			alert(response +'3');
		});
	});
	
	//jqXHR 就是 $.ajax() 返回的对象
	$('form input[type=button]').click(function(){
		var jqXHR1 = $.ajax('test1.php');
		var jqXHR2 = $.ajax('test2.php');
		jqXHR1.done(function(response){
			alert(response);
		});
		jqXHR2.done(function(response){
			alert(response);
		});
		
	});
	
	//jqXHR 就是 $.ajax() 返回的对象
	$('form input[type=button]').click(function(){
		var jqXHR1 = $.ajax('test1.php');
		var jqXHR2 = $.ajax('test2.php');
		$.when(jqXHR1,jqXHR2).done(function(r1,r2){
			alert(r1[0]);
			alert(r2[0]);
		});
	});
	
	*/
});

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics