Light12306/Mobile12306New/xhrdemo.html
2014-08-21 14:15:52 +08:00

158 lines
4.9 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<h3>说明</h3>
<p>
调用服务器接口:<strong>__TicketJavaScriptObject__.sendRequest(properties)</strong>
</p>
<p>properties可能包含以下属性</p>
<p>1. id (int) 当前请求id</p>
<p>2. url (string) 地址</p>
<p>3. method (string) 方法 GET/POST</p>
<p>4. headers (json object) 请求头</p>
<p>5. postdata (string) POST数据</p>
<p>6. returnType (string) 返回类型text/image</p>
<p>6. callback 返回时调用的回调函数名</p>
<p>7. requestCharset (string) 请求和响应的编码</p>
<p></p>
<p>回调函数原型</p>
<p>fishXhrLoadCallback(object)</p>
<p>object包括的内容</p>
<p>1. success (bool) 成功或失败</p>
<p>2. headers (object) 返回的请求头</p>
<p>3. result (base64/string) 返回的结果。请求类型为text时返回字符串。请求类型为image时返回数据的base64结果</p>
<p>4. id (int) 对应的请求ID</p>
<p>5. statusCode (int) 请求的状态码</p>
<p>6. statusDescription (string) 请求的状态码说明</p>
<p></p>
<p><strong>注意需要自动处理Cookies等状态信息网页端不会对这些信息进行维护在返回给客户端之前代理类需要完成对这些状态的维护。</strong></p>
<p>返回的信息类型默认为string。网页端调用如果指定了returnType则强制为此类型。如果没有制定返回类型则自动猜测。在此约定所以返回的ContentType为 image/ 的均按returnType为image处理否则均返回text</p>
<p>请求的编码一般不设置请根据返回类型的Charset猜测。如果没有相关信息请默认为 UTF-8</p>
<h3>测试</h3>
<div id="result"></div>
<button id="loadText">测试加载文本</button>
<button id="loadImage">测试加载图片</button>
<script src="js/zepto.js"></script>
<script>
var lbAjax = (function () {
var queue = {};
var callid = 0;
window.fishXhrLoadCallback = function (obj) {
if (typeof (obj) === "string")
obj = JSON.parse(obj);
var def = queue[obj.id];
if (!def)
return;
//process with json
if (def.rawResultType === "json") {
try {
obj.result = JSON.parse(obj.result);
} catch (e) {
obj.success = false;
}
}
delete queue[obj.id];
if (obj.success) {
def.resolve(obj.result, {
headers: obj.headers,
statusCode: obj.statusCode,
statusDescription: obj.statusDescription,
id: obj.id
});
} else {
def.reject(obj.result, {
headers: obj.headers,
statusCode: obj.statusCode,
statusDescription: obj.statusDescription,
id: obj.id
});
}
};
var ajax = function (method, url, returnType, postdata, refer, headers) {
var def = new $.Deferred();
postdata = postdata || "";
if (typeof (postdata) !== "string")
postdata = $.param(postdata);
headers = headers || {};
if (refer) {
headers = $.extend({}, headers, { Referer: refer });
}
def.rawResultType = returnType || "json";
def.context = {
id: ++callid,
url: url,
method: method,
postdata: postdata,
refer: refer,
headers: headers || {},
callback: "fishXhrLoadCallback",
requestCharset: "UTF-8",
returnType: def.rawResultType === "image" ? "image" : "text"
};
queue[def.context.id] = def;
window.__TicketJavaScriptObject__.sendRequest(JSON.stringify(def.context));
return def.promise();
};
return {
ajax: ajax,
get: function () {
var arr = [].slice.call(arguments);
arr.unshift("GET");
return ajax.apply(this, arr);
},
post: function () {
var arr = [].slice.call(arguments);
arr.unshift("POST");
return ajax.apply(this, arr);
},
getImage: function(url, refer) {
return ajax("GET", url, "image", null, refer);
}
}
})();
$(function () {
var div = $("#result");
if (!window.__TicketJavaScriptObject__) {
div.html("错误:无法调用 window.__TicketJavaScriptObject__");
$("button").prop("disabled", true);
}
$("#loadImage").click(function () {
lbAjax.getImage("https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand", "https://kyfw.12306.cn/otn/login/init")
.done(function (data) {
alert(data);
$("#result").html("加载成功!<img src='data:image/png;base64," + data + "' />");
}).fail(function (data) {
$("#result").html("加载失败!" + data);
});
});
$("#loadText").click(function () {
lbAjax.get("https://kyfw.12306.cn/otn/login/init", "text", null, "https://kyfw.12306.cn/otn/")
.done(function (data) {
$("#result").text("加载成功!" + data);
}).fail(function (data) {
$("#result").text("加载失败!" + data);
});
});
});
</script>
</body>
</html>