最近在项目中遇到一个非常棘手的问题,就是在为页面设置了document.domain时,ajaxfileupload出现跨域错误,究其原因是页面的domain被设置为***.com时,而ajaxfileupload post to 的页面的域并非根域***.com,而是一个二级的类似abc.***.com的域名,因此会引发无权限的错误。
花了2天的时间终于解决这个问题。
修改后的ajaxfileupload关键代码如下:
var uploadCallback = function(isTimeout) {
var io = document.getElementById(frameId);
var execontent = function(){
try {
if (io.contentWindow) {
xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML: null;
xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument: io.contentWindow.document;
} else if (io.contentDocument) {
xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML: null;
xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument: io.contentDocument.document;
}
} catch(e) {
handleError(s, xml, null, e);
}
if (xml || isTimeout == "timeout") {
requestDone = true;
var status;
try {
status = isTimeout != "timeout" ? "success": "error";
// Make sure that the request was successful or notmodified
if (status != "error") {
// process the data (runs the xml through httpData regardless of callback)
var data = jQuery.uploadHttpData(xml, s.dataType);
// If a local callback was specified, fire it and pass it the data
if (s.success) s.success(data, status);
// Fire the global callback
if (s.global) jQuery.event.trigger("ajaxSuccess", [xml, s]);
} else handleError(s, xml, status);
} catch(e) {
status = "error";
handleError(s, xml, status, e);
}
// The request was completed
if (s.global) jQuery.event.trigger("ajaxComplete", [xml, s]);
// Handle the global AJAX counter
if (s.global && !--jQuery.active) jQuery.event.trigger("ajaxStop");
// Process result
if (s.complete) s.complete(xml, status);
jQuery(io).unbind();
setTimeout(function() {
try {
jQuery(io).remove();
jQuery(form).remove();
} catch(e) {
handleError(s, xml, null, e);
}
},
100);
xml = null;
}
};
var crossdomain = !jQuery.browser.msie || document.domain == location.hostname;
if(!crossdomain) {
io.src="javascript:try{"
+"document.domain=window.location.hostname.split('.').reverse().slice(0,2).reverse().join('.');"
+"parent.document.getElementById('"+ frameId +"').setAttribute('uploaded','uploaded');"
+"}catch(e){}";
var timer = window.setInterval(function(){
try{
if(io.getAttribute("uploaded") == "uploaded") {
crossdomain = true;
window.clearInterval(timer);
execontent();
}
}catch(e){}
},1000);
} else {
execontent();
}
};
解决问题的思路是,如果是IE浏览器并且为当前页面设置了document.domain,那么在创建的iframe提交后,使用iframe的src属性执行一段js代码,从而改变该iframe页面的domain属性,达到同域的目的。
http://www.qingniao.it/index.php?title=Jquery%E6%8F%92%E4%BB%B6ajaxfileupload%E7%9A%84%E4%B8%80%E6%AC%A1%E8%B7%A8%E5%9F%9F%E4%BD%93%E9%AA%8C
分享到:
相关推荐
jquery ajaxfileupload上传插件,用于ajax的异步文件上传
jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个demo演示怎么用AjaxFileUpload插件实现文件上传。 1、引入AjaxFileUpload插件相关的js 复制代码 代码如下:[removed]resources/js/...
jQuery插件AjaxFileUpload用来实现ajax文件上传,该插件使用非常简单,接下来写个demo演示怎么用AjaxFileUpload插件实现文件上传。
JSP使用ajaxFileUpload.js实现跨域问题.docx
主要介绍了一个简单的jQuery插件ajaxfileupload.js实现ajax上传文件例子,需要的朋友可以参考下
jquery ajaxfileupload.js异步上传插件
jQuery的ajaxFileUpload.js。感觉这种异步上传的方式非常好用
通过修改终于使得ajaxfileupload兼容jquery3,代码已上线运行。
jQuery.AjaxFileUpload.js, jQuery插件神奇地让文件输入通过ajax上传 这个插件上传文件 somehwere,并将响应传递给回调,无其他。它不依赖于特定的HTML,只需给它一个 <input type="file">它不要求你的服务器...
NULL 博文链接:https://aokunsang.iteye.com/blog/1014274
jquery+ajaxfileupload+html文件上传,用ajaxfileupload插件做的上传文件
折腾了一天,亲自调试完成亲测jquery+ajaxfileupload+demo
主要介绍了jQuery插件ajaxfileupload.js实现上传文件的相关资料,感兴趣的小伙伴们可以参考一下
主要介绍了Asp.net MVC中使用JQuery插件ajaxFileUpload上传文件,需要的朋友可以参考下
主要为大家详细介绍了jQuery插件ajaxFileUpload使用教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要介绍了PHP结合jQuery插件ajaxFileUpload实现异步上传文件实例,感兴趣的小伙伴们可以参考一下
主要介绍了jQuery插件ajaxFileUpload异步上传文件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要为大家详细介绍了jQuery插件ajaxFileUpload使用实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
现在网上下载的上传插件(ajaxfileupload.js)很多都不支持通过设置data来传参,我这里改了一下。