-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathajax.txt
304 lines (249 loc) · 8.89 KB
/
ajax.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
ajaxSettings: {
url: location.href,
type: "GET",
isLocal: rlocalProtocol.test( location.protocol ),
global: true,
processData: true,
async: true,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
/*
timeout: 0,
data: null,
dataType: null,
username: null,
password: null,
cache: null,
throws: false,
traditional: false,
headers: {},
*/
accepts: {
"*": allTypes,
text: "text/plain",
html: "text/html",
xml: "application/xml, text/xml",
json: "application/json, text/javascript"
},
contents: {
xml: /\bxml\b/,
html: /\bhtml/,
json: /\bjson\b/
},
responseFields: {
xml: "responseXML",
text: "responseText",
json: "responseJSON"
},
// Data converters
// Keys separate source (or catchall "*") and destination types with a single space
converters: {
// Convert anything to text
"* text": String,
// Text to html (true = no transformation)
"text html": true,
// Evaluate text as a json expression
"text json": JSON.parse,
// Parse text as xml
"text xml": jQuery.parseXML
},
// For options that shouldn't be deep extended:
// you can add your own custom options here if
// and when you create one that shouldn't be
// deep extended (see ajaxExtend)
flatOptions: {
url: true,
context: true
}
},
-------------------------------------------------------
$.ajax({
type: "GET",
url: "test.json",
data: {username:$("#username").val(), content:$("#content").val()},
dataType: "json",
success: function(data){
$('#resText').empty(); //清空resText里面的所有内容
var html = '';
$.each(data, function(commentIndex, comment){
html += '<div class="comment"><h6>' + comment['username']
+ ':</h6><p class="para"' + comment['content']
+ '</p></div>';
});
$('#resText').html(html);
}
});
/* 加载热销商品列表页*/
$.ajax({
url:"http://datainfo.duapp.com/shopdata/getGoods.php?callback=?",
type:"get",
dataType:"jsonp",
success:function(resp) {
console.log(resp);
for(var i = 0; i < resp.length; i ++) {
// DOM操作添加商品
var $goodsBox = $("<div>").addClass("col-md-3").addClass("col-sm-4");
var $thumbnail = $("<div>").addClass("thumbnail");
var $img = $("<img>").attr("src", resp[i].goodsListImg);
var $caption = $("<div>").addClass("caption");
var $priceSpan = $("<span>").text(resp[i].price);
var $price = $("<h3>").text("¥").append($priceSpan);
var $name = $("<p>").text(resp[i].goodsName);
var $id = $("<p>").text(resp[i].goodsID).css("visible", "hidden");
var $addCart = $("<button>").addClass("btn").addClass("btn-danger").text("加入购物车");
var $buyNow = $("<button>").addClass("btn").addClass("btn-primary").text("立即购买");
$caption.append($price);
$caption.append($name);
$caption.append($id);
$thumbnail.append($img);
$thumbnail.append($caption);
$thumbnail.append($addCart);
$thumbnail.append($buyNow);
$goodsBox.append($thumbnail)
$(".hotgoodslist").append($goodsBox);
}
}
});
/*
加载衬衫商品列表页
*/
$.ajax({
url:"http://datainfo.duapp.com/shopdata/getGoods.php?callback=?",
type:"get",
data:{
classID:1
},
dataType:"jsonp",
success:function(resp) {
console.log(resp);
for(var i = 0; i < resp.length; i ++) {
// DOM操作添加商品
var $goodsBox = $("<div>").addClass("col-md-3").addClass("col-sm-4");
var $thumbnail = $("<div>").addClass("thumbnail");
var $img = $("<img>").attr("src", resp[i].goodsListImg);
var $caption = $("<div>").addClass("caption");
var $priceSpan = $("<span>").text(resp[i].price);
var $price = $("<h3>").append($priceSpan);
var $name = $("<p>").text(resp[i].goodsName);
var $id = $("<p>").text(resp[i].goodsID).css("visible", "hidden");
$caption.append($price);
$caption.append($name);
$caption.append($id);
$thumbnail.append($img);
$thumbnail.append($caption);
$goodsBox.append($thumbnail)
$(".shirt").append($goodsBox);
}
}
});
$.ajax({url:"/jquery/test1.txt",async:false});
$.ajax({ url: "test.html", context: document.body, success: function(){
$(this).addClass("done");
}});
-------------------------------------------------------------
$.post("demo_ajax_gethint.asp",{suggest:txt},function(result){
$("span").html(result);
});
$.post("ajax/test.html", function(data) {
$(".result").html(data);
});
// 请求生成后立即分配处理程序,请记住该请求针对 jqxhr 对象
var jqxhr = $.post("example.php", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
// 在这里执行其他任务
// 为上面的请求设置另一个完成函数
jqxhr.complete(function(){ alert("second complete"); });
$.post("test.php", { name: "John", time: "2pm" } );
向服务器传递数据数组(同时仍然忽略返回值):
$.post("test.php", { 'choices[]': ["Jon", "Susan"] });
例子 3
使用 ajax 请求发送表单数据:
$.post("test.php", $("#testform").serialize());
例子 4
输出来自请求页面 test.php 的结果(HTML 或 XML,取决于所返回的内容):
$.post("test.php", function(data){
alert("Data Loaded: " + data);
});
例子 5
向页面 test.php 发送数据,并输出结果(HTML 或 XML,取决于所返回的内容):
$.post("test.php", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});
例子 6
获得 test.php 页面的内容,并存储为 XMLHttpResponse 对象,并通过 process() 这个 JavaScript 函数进行处理:
$.post("test.php", { name: "John", time: "2pm" },
function(data){
process(data);
}, "xml");
例子 7
获得 test.php 页面返回的 json 格式的内容:
$.post("test.php", { "func": "getNameAndTime" },
function(data){
alert(data.name); // John
console.log(data.time); // 2pm
}, "json");
-------------------------------------------------------------------------------------
实例
使用 AJAX 的 GET 请求来改变 div 元素的文本:
$("button").click(function(){
$.get("demo_ajax_load.txt", function(result){
$("div").html(result);
});
});
亲自试一试
定义和用法
get() 方法通过远程 HTTP GET 请求载入信息。
这是一个简单的 GET 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。
语法
$(selector).get(url,data,success(response,status,xhr),dataType)
参数 描述
url 必需。规定将请求发送的哪个 URL。
data 可选。规定连同请求发送到服务器的数据。
success(response,status,xhr)
可选。规定当请求成功时运行的函数。
额外的参数:
response - 包含来自请求的结果数据
status - 包含请求的状态
xhr - 包含 XMLHttpRequest 对象
dataType
可选。规定预计的服务器响应的数据类型。
默认地,jQuery 将智能判断。
可能的类型:
"xml"
"html"
"text"
"script"
"json"
"jsonp"
详细说明
该函数是简写的 Ajax 函数,等价于:
$.ajax({
url: url,
data: data,
success: success,
dataType: dataType
});
根据响应的不同的 MIME 类型,传递给 success 回调函数的返回数据也有所不同,这些数据可以是 XML root 元素、文本字符串、JavaScript 文件或者 JSON 对象。也可向 success 回调函数传递响应的文本状态。
对于 jQuery 1.4,也可以向 success 回调函数传递 XMLHttpRequest 对象。
示例
请求 test.php 网页,忽略返回值:
$.get("test.php");
更多示例
例子 1
请求 test.php 网页,传送2个参数,忽略返回值:
$.get("test.php", { name: "John", time: "2pm" } );
例子 2
显示 test.php 返回值(HTML 或 XML,取决于返回值):
$.get("test.php", function(data){
alert("Data Loaded: " + data);
});
例子 3
显示 test.cgi 返回值(HTML 或 XML,取决于返回值),添加一组请求参数:
$.get("test.cgi", { name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
});