Jquery php Json Ajax POST GET
来源:互联网 发布:ehr人力资源软件 编辑:程序博客网 时间:2024/05/22 06:21
jQuery异步加载应用越来越多,可以有效解决一次性数据加载量过大的问题,提高网页加载速度,提升用户使用感受,也可以不切换页面做到更新数据的效果。异步加载的方法包括GET、POST、AJAX等,可以加载静态页面,也可以加载动态页面(PHP)的、json、xml数据。
jQuery API的介绍的写法:
1
//$post
2
$.post(url, [data], [callback], [type])
3
//$get
4
$.get(url, [data], [callback], [type])
5
//$ajax
6
$.ajax([options])
例子: 点击标题,向后翻页论坛主题列表:
原理: 将div id forum-49拆分后,得到版块ID: 49,然后通过数据库查询,提取fid=49的主题列表。
HTML、Javascript代码部分:(保存为bbs.html文件)
01
<head>
02
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=UTF-8"
/>
03
<title>jQuery GET POST AJAX與php异步加载 - 聖騎天下</title>
04
<style type=
"text/css"
>
05
.forum{margin:0px auto;width:800px;height:80px;background:#ea2;text-align:center;font-size:60px;}
06
#posts-49{margin:0px auto;width:800px;height:500px;background:#eee;}
07
#posts-49 .info{margin:0px auto;width:800px;height:20px;background:#e99;text-align:right;}
08
#posts-49 .content{margin:0px auto;width:800px;height:450px;background:#eee;}
09
</style>
10
</head>
11
<body>
12
<div
class
=
"forum"
id=
"forum-49"
>forum id : 49</div>
13
<div id=
"posts-49"
>
14
<div
class
=
"info"
> </div>
15
<div
class
=
"content"
> </div>
16
</div>
17
<script type=
"text/javascript"
src=
"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"
></script>
18
<script type=
"text/javascript"
>
19
$().ready(
function
(){
20
$(
'.forum'
).click(
function
(){
21
var
arr=
new
Array();
22
var
forumIDdiv = $(this).attr(
"id"
);
23
var
arr = forumIDdiv.split(
'-'
);
24
forumIDdiv = arr[1];
25
changeData(forumIDdiv);
26
})
27
var
page=0;
28
function
changeData( fid ){
29
page++;
30
$.ajax({
31
type:
'GET'
,
32
url:
'bbs.php'
,
33
data:{fid:fid,page:page},
//url和data等价,url:'bbs.php?fid='+fid+'&page='+page
34
contentType:
'text/html;charset=utf-8'
,
//编码格式
35
beforeSend:
function
(data){
36
$(
'#posts-'
+fid+
' .info'
).html(
'loading...'
);
37
},
//发送请求前
38
success:
function
(data){
39
$(
'#posts-'
+fid+
' .content'
).html(data);
40
//alert(data);
41
},
//请求成功后
42
error:
function
(data){
43
$(
'#posts-'
+fid+
' .info'
).html(
'failed to load data.'
)
44
},
//请求错误
45
complete:
function
(data){
46
$(
'#posts-'
+fid+
' .info'
).html(
'Loaded fid: '
+fid+
' | page: '
+page+
' ...'
);
47
}
//请求完成
48
});
49
50
}
51
})
52
</script>
53
</body>
php代码部分:(保存为bbs.php文件)
01
<?php
02
header(
'Content-Type:text/html;charset=utf-8'
);
03
error_reporting
(0);
04
set_magic_quotes_runtime(0);
05
if
(PHP_VERSION <
'4.1.0'
) {
06
$_GET
= &
$HTTP_GET_VARS
;
07
$_POST
= &
$HTTP_POST_VARS
;
08
$_COOKIE
= &
$HTTP_COOKIE_VARS
;
09
$_SERVER
= &
$HTTP_SERVER_VARS
;
10
$_ENV
= &
$HTTP_ENV_VARS
;
11
$_FILES
= &
$HTTP_POST_FILES
;
12
}
13
foreach
(
array
(
'_COOKIE'
,
'_POST'
,
'_GET'
)
as
$_request
) {
14
foreach
($
$_request
as
$_key
=>
$_value
) {
15
$_key
{0} !=
'_'
&& ($
$_key
= ${
$_request
}[
$_key
] = daddslashes(
$_value
));
16
}
17
}
18
if
(
empty
(
$page
))
$page
= 1;
if
(
empty
(
$fid
))
$fid
= 3;
19
$page
= max(1,
intval
(
$page
));
$fid
= max(3,
intval
(
$fid
)) ;
20
$fpage
= 30;
$tpage
= 15;
21
$start
= (
$page
-1) *
$fpage
;
22
$db
= mysql_connect(
'www.opensoce.com'
,
'root'
,
'123456'
);
23
if
(!
$db
){
24
echo
'数据库连接失败!'
;
25
exit
;
26
}
27
mysql_query(
"SET NAMES utf8"
,
$db
);
28
mysql_query(
"USE opensoce"
,
$db
);
29
if
(isset(
$fid
)) {
30
$qry
= mysql_query(
"select tid,subject from cdb_threads where fid='$fid' order by lastpost desc limit $start, $fpage"
,
$db
);
//按照最后回复顺序取出列表
31
if
(
$qry
) {
32
while
(
$r
= mysql_fetch_array(
$qry
)) {
33
echo
"<a href=
'viewthread.php?tid=$r[tid]'
>
$r
[subject]</a><br />';
34
}
35
}
else
{
36
echo
'本版无更多主题。'
;
37
}
38
}
else
{
39
echo
'no'
;
40
}
41
42
mysql_close(
$db
);
43
function
daddslashes(
$string
,
$force
= 0) {
44
!defined(
'MAGIC_QUOTES_GPC'
) && define(
'MAGIC_QUOTES_GPC'
, get_magic_quotes_gpc());
45
if
(!MAGIC_QUOTES_GPC ||
$force
) {
46
if
(
is_array
(
$string
)) {
47
foreach
(
$string
as
$key
=>
$val
) {
48
$string
[
$key
] = daddslashes(
$val
,
$force
);
49
}
50
}
else
{
51
$string
=
addslashes
(
$string
);
52
}
53
}
54
return
$string
;
55
}
56
?>
参数详细介绍:
- $post
1
url (String) : 发送请求的URL地址.
2
data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示。
3
callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
4
type (String) : (可选)官方的说明是:Type of data to be sent。其实应该为客户端请求的类型(JSON,XML,等等)
- $get
1
url (String) : 发送请求的URL地址.
2
data (Map) : (可选) 要发送给服务器的数据,以 Key/value 的键值对形式表示,会做为QueryString附加到请求URL中。
3
callback (Function) : (可选) 载入成功时回调函数(只有当Response的返回状态是success才是调用该方法)。
- $ajax
参数类型描述url String(默认: 当前页地址) 发送请求的地址。typeString请求方式:"POST" 或 "GET", 默认 "GET"。注意: 其它 HTTP 请求方法, 如 PUT 和 DELETE 也可以使用,但仅部分浏览器支持。timeoutNumber设置请求超时时间(毫秒)。此设置将覆盖全局设置。async Boolean(默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。beforeSend Function发送请求前可修改 XMLHttpRequest 对象的函数,如添加自定义 HTTP 头。XMLHttpRequest 对象是唯一的参数。cache Boolean(默认: true) jQuery 1.2 新功能, 设置为 false 将不会从浏览器缓存中加载请求信息。complete Function请求完成后回调函数 (请求成功或失败时均调用)。参数: XMLHttpRequest 对象,成功信息字符串。1
function
(XMLHttpRequest) {
2
this;
//ajax opration
3
}
contentType String(默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数应用场合。data Object,1
function
(XMLHttpRequest, textStatus) {
2
this;
//ajax oprations
3
}
String发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 ‘&foo=bar1&foo=bar2′。dataType String预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText,并作为回调函数参数传递,可用值:
error Function(默认: 自动判断 (xml 或 html)) 请求失败时将调用此方法。这个方法有三个参数:XMLHttpRequest 对象,错误信息,(可能)捕获的错误对象。1
"xml"
: 返回 XML文档, 可用 jQuery处理。
2
"html"
: 返回文本HTML信息; 含script元素。
3
"script"
:返回文本JavaScript代码;不自动缓存结果
4
"json"
: 返回 JSON 数据 。
5
"jsonp"
: JSONP 格式。使用JSONP 形式调用函数时,如
"myurl?callback=?"
jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。
global Boolean(默认: true) 是否触发全局 AJAX 事件。设置为 false 将不会触发全局 AJAX 事件,如 ajaxStart 或 ajaxStop 。可用于控制不同的Ajax事件ifModified Boolean(默认: false) 仅在服务器数据改变时获取新数据。使用 HTTP 包 Last-Modified 头信息判断。processData Boolean(默认: true) 默认情况下,发送的数据将被转换为对象(技术上讲并非字符串) 以配合默认内容类型 "application/x-www-form-urlencoded"。如果要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。success Function请求成功后回调函数。这个方法有两个参数:服务器返回数据,返回状态1
function
(XMLHttpRequest, textStatus, errorThrown) {
2
//通常textStatus和errorThown只其中一个有值
3
this;
//ajax oprations
4
}
1
function
(data, textStatus) {
2
//data: xmlDoc, jsonObj, html, text, etc...
3
this;
//ajax oprations
4
}
一些补充说明:
- $.load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中。
1
url (String) : 请求的HTML页的URL地址。
2
data (Map) : (可选) 发送至服务器的 key/value 数据。
3
callback (Callback) : (可选) 请求完成时(不需是success的)的回调函数。
- $.getScript( url, [callback] ) : 通过 GET 方式请求载入并执行一个 JavaScript 文件。
1
url (String) : 待载入 JS 文件地址。
2
callback (Function) : (可选) 成功载入后回调函数。
- Jquery php Json Ajax POST GET
- jquery ajax post get
- jQuery $ajax $post $get
- Jquery $.ajax/$.post/$.get
- jQuery:$post、$get、$ajax与php,实现异步加载
- jQuery:$post、$get、$ajax与php,实现异步加载
- JQuery .ajax .post .get 应用
- jQuery $.ajax() $.get() $.post() $.getJSON()
- jQuery-ajax-.get,.post方法
- jquery ajax的get post
- Jquery:ajax post get提交
- jquery ajax post json对象
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- botp生成目标数据,并建立上查下查关系
- SlidingupPanel
- 动态代理--java实现
- 为Amazon EC2 Instance 设置允许ping
- 免费 webservice 收集
- Jquery php Json Ajax POST GET
- 基本语言细节--《The C++ Programming Language 》--(2)类型和声明
- .owner = THIS_MODULE为什么加“点”的原因
- Java中的中文编码问题
- ENGLISH资料收集(31)-最近“忙”吗
- java ImageIO处理
- 小知识点_20131128
- linux inet_aton inet_ntoa
- C++ 文件操作