提交 | 用户 | 时间
|
8286c6
|
1 |
layui.define(['jquery'], function (exports) { |
懒 |
2 |
var $ = layui.$; |
|
3 |
|
|
4 |
var $ax = function (url, success, error) { |
|
5 |
this.url = url; |
|
6 |
this.type = "post"; |
|
7 |
this.data = {}; |
|
8 |
this.dataType = "json"; |
|
9 |
this.async = false; |
|
10 |
this.success = success; |
|
11 |
this.error = error; |
|
12 |
}; |
|
13 |
|
|
14 |
$ax.prototype = { |
|
15 |
start: function () { |
|
16 |
var me = this; |
|
17 |
var result = ""; |
|
18 |
|
|
19 |
if (this.url.indexOf("?") === -1) { |
|
20 |
this.url = this.url + "?jstime=" + new Date().getTime(); |
|
21 |
} else { |
|
22 |
this.url = this.url + "&jstime=" + new Date().getTime(); |
|
23 |
} |
|
24 |
|
|
25 |
$.ajax({ |
|
26 |
type: me.type, |
|
27 |
url: me.url, |
|
28 |
dataType: me.dataType, |
|
29 |
async: me.async, |
|
30 |
data: me.data, |
|
31 |
beforeSend: function (data) { |
|
32 |
|
|
33 |
}, |
|
34 |
success: function (data) { |
|
35 |
result = data; |
|
36 |
if (me.success !== undefined) { |
|
37 |
me.success(data); |
|
38 |
} |
|
39 |
}, |
|
40 |
error: function (data) { |
|
41 |
if (me.error !== undefined) { |
|
42 |
me.error(data); |
|
43 |
} |
|
44 |
} |
|
45 |
}); |
|
46 |
|
|
47 |
return result; |
|
48 |
}, |
|
49 |
|
|
50 |
set: function (key, value) { |
|
51 |
if (typeof key === "object") { |
|
52 |
for (var i in key) { |
|
53 |
if (typeof i === "function") |
|
54 |
continue; |
|
55 |
this.data[i] = key[i]; |
|
56 |
} |
|
57 |
} else { |
|
58 |
this.data[key] = (typeof value === "undefined") ? $("#" + key).val() : value; |
|
59 |
} |
|
60 |
return this; |
|
61 |
}, |
|
62 |
|
|
63 |
setData: function (data) { |
|
64 |
this.data = data; |
|
65 |
return this; |
|
66 |
}, |
|
67 |
|
|
68 |
clear: function () { |
|
69 |
this.data = {}; |
|
70 |
return this; |
|
71 |
} |
|
72 |
}; |
|
73 |
|
|
74 |
exports('ax', $ax); |
|
75 |
}); |