栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

jQuery等同于XMLHttpRequest的上载吗?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery等同于XMLHttpRequest的上载吗?

这是我想出的解决此问题的方法。$ .ajax()调用允许提供回调以生成XHR。我只是在调用请求之前生成一个请求,进行设置,然后创建一个闭包以在$
.ajax()需要它时将其返回。如果他们只是通过jqxhr授予访问权限,那会容易得多,但事实并非如此。

var reader = new FileReader();reader.onloadend = function (e) {    var xhr, provider;    xhr = jQuery.ajaxSettings.xhr();    if (xhr.upload) {        xhr.upload.addEventListener('progress', function (e) { // ...        }, false);    }       provider = function () {        return xhr;    };    // Leave only the actual base64 component of the 'URL'    // Sending as binary ends up mangling the data somehow    // base64_depre() on the PHP side will return the valid file.    var data = e.target.result;    data = data.substr(data.indexOf('base64') + 7);    $.ajax({        type: 'POST',        url: 'http://example.com/upload.php',        xhr: provider,        dataType: 'json',        success: function (data) { // ...        },          error: function () { // ...        },          data: { name: file.name, size: file.size, type: file.type, data: data,        }       }); };  reader.readAsDataURL(file);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404107.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号