使用YUI3 IO组件实现ajax
来源:程序员人生 发布时间:2014-01-12 18:27:27 阅读次数:2432次
注意:如下写法不能实现ajax跨域,连子域跨域都不行。
/*
*<div id="show_info"></div>
*<input type="button" id="requestButton" value="Send a POST Request">
*/
YUI().use("io", function(Y){
var show_info = Y.one('#show_info');
var handleSuccess = function(ioId, o){
if (o.responseText !== undefined) {
var s = "<li>Transaction id: " + ioId + "</li>";
s += "<li>HTTP status: " + o.status + "</li>";
s += "<li>Status code message: " + o.statusText + "</li>";
s += "<li>HTTP headers received: <ul>" + o.getAllResponseHeaders() + "</ul></li>";
s += "<li>page response: " + o.responseText + "</li>";
show_info.set("innerHTML", s);
}
}
var handleFailure = function(ioId, o){
if (o.responseText !== undefined) {
show_info.set("innerHTML", "handleFailure");
}
}
Y.on('io:success', handleSuccess);
Y.on('io:failure', handleFailure);
var cfg = {
method: "POST",
data: "user=fengyue&password=888",
//data: ""
};
var sUrl = "http://www.alimama.net:8080/union/aa.htm";
function makeRequest(){
show_info.set("innerHTML", "Loading data from new request...");
var request = Y.io(sUrl, cfg);
}
Y.on("click", makeRequest, "#requestButton");
});
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠