-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
212 lines (189 loc) · 8.29 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta charset="utf-8">
</style>
<title></title>
</head>
<body>
<img src="./image/source/web.jpg" alt="图片转换" id = 'item'/>
</body>
<script type="text/javascript">
function ajax(){
var xhr, // 全局变量
Ajax = function (params){
this.settings = {
url: "",
datatype: "",
async: true, // 默认异步请求
method: "",
data: {}
};
this.sendData(params);
};
// 原型方法
Ajax.prototype = {
constructor: Ajax,
// 创建XHR对象
createXHR: function (){
if(typeof XMLHttpRequest != "undefined"){
return new XMLHttpRequest();
}else if(typeof ActiveXObject != "undefined"){
// 针对IE7之前版本
if(typeof arguments.callee.activeXString != "string"){
// IE中三个不同版本XHR对象
var versions = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"],
i,
len;
for(i = 0,len = versions.length; i < len; i++){
try{
new ActiveXObject(versions[i]); // 实例化IE的XHR对象
arguments.callee.activeXString = versions[i];
break;
}catch (ex){
//
}
}
}
return new ActiveXObject(arguments.callee.activeXString);
}else{
throw new Error("No XHR Object Available");
}
},
sendData: function (params){
xhr = new this.createXHR(); // 实例化 XMLHttpRequest
params = params || {};
params.data = params.data || {};
if(params.url && params.datatype && params.method){
this.settings.url = params.url;
this.settings.datatype = params.datatype;
this.settings.data = params.data;
this.settings.method = params.method;
}else{
console.log("The params has wrong!");
return;
}
// 获取状态码
// 检测异步请求
function complete(){
if(xhr.readyState == 4){
if((xhr.status >= 200 && xhr.status <300) || xhr.status == 304){
// 未对返回的JSON做处理
// 勿用 statusText == 'success' 做判定条件 不适用跨浏览器场景
if(params.success){
// 设置回调函数
params.success(xhr.responseText);
}
}
}
}
// 同源请求 [GET] JSON
if(this.settings.datatype == 'json' || this.settings.datatype == 'JSON'){
if(this.settings.method == 'get' || this.settings.method == 'GET'){
// GET请求
for(var item in this.settings.data){
this.settings.url = this.addURLParam(this.settings.url,item,this.settings.data[item]);
}
this.settings.url+= (this.settings.url.indexOf('?'))=== -1 ? '?number=' + Math.random() : '&number=' + Math.random() ;
xhr.onreadystatechange = complete;
xhr.open(this.settings.method,this.settings.url,this.settings.async);
xhr.send(null);
}
// POST请求
if(this.settings.method == 'post' || this.settings.method == 'POST'){
xhr.onreadystatechange = complete;
xhr.open(this.settings.method,this.settings.url,this.settings.async);
xhr.setRequestHeader ("Content-Type","application/json");
xhr.send(this.settings.data);
}
}
// 非同源JSONP
if(this.settings.datatype == 'jsonp' || this.settings.datatype == 'JSONP'){
if(this.settings.method == 'get' || this.settings.method == 'GET'){
var callbackName = params.JSONP, // 设置jsonp的回调函数名
head = document.getElementsByTagName('head')[0],
scriptJsonp = document.createElement('script');
this.settings.data.callback = callbackName;
head.appendChild(scriptJsonp);
// 创建JSONP的回调函数
window[callbackName] = function (JSON){
head.removeChild(scriptJsonp);
clearTimeout(scriptJsonp.timer);
window[callbackName] = null;
if(params.success){
// 设置回调函数
params.success(JSON);
}
};
// 设置超时处理
if(params.timeout){
scriptJsonp.timer = setTimeout(function () {
head.removeChild(scriptJsonp);
if(params.error){
// 请求超时 手动设置返回数据
params.error({message: "Request is Timeout"});
}
window[callbackName] = null;
}, params.timeout);
}
// 处理JSONP跨域请求URL
this.settings.url = this.settings.url + "?callback=" + callbackName;
for(var src in this.settings.data){
this.settings.url=this.addURLParam(this.settings.url,src,this.settings.data[src]);
}
// 设置请求随即会自动发送请求
scriptJsonp.src = this.settings.url;
}
}
},
// 处理URL
addURLParam: function (url,name,value){
url += (url.indexOf("?") == -1 ? "?" : "&");
url += encodeURIComponent(name)+"="+encodeURIComponent(value);
return url;
},
// 序列化
serialize: function (data){
var val="";
var str="";
for(var item in data){
str=item+"="+data[item];
val+=str+'&';
}
return val.slice(0,val.length-1);
}
};
return {
init: function (params){
new Ajax(params);
}
};
}
var Ajax = ajax(),
item = document.getElementById('item'),
src;
src = item.getAttribute('src');
var data = {
"source": src,
"width": 794,
"height": 1123,
"h1": "This is a PDF",
"p": "It be made node-canvas,It be made node-canvas,It be made node-canvas,It be made node-canvas",
"name": "test.pdf",
"outUrl": "./image/newImg/",
"type": "pdf"
};
data = JSON.stringify(data);
Ajax.init({
url: "http://127.0.0.1:3000/imgtrans",
datatype: "json",
data: data,
method: "POST",
success: function (result){
result = JSON.parse(result);
item.src = result.src;
}
})
</script>
</html>