• kindeditor编辑器上传图片跨域


    项目通常引入的是kindeditor-all.min.js,这里我们需要改为引入kindeditor-all.js,因为要对其源码进行修改。

    1.打开文件,搜索下面的这行代码:

    KindEditor.plugin('image', function(K) {

    2.查找下面提交图片办法,并将其注释掉,因为会出现跨域问题:

    //uploadbutton.submit();

    3.把下边的代码粘在上一行代码的后边:

    var formData = new FormData();
    var file=uploadbutton.fileBox[0].files[0];
    formData.append(file.name, file);
    //console.log(file,formData)
    K.ajaxForm(self.options.uploadJson, function(data) {
        dialog.hideLoading();
        //console.log(data);
        if (data.error==0) {
            //console.log(self.options);
            var html = '<img src="' + self.options.basePath + data.url + '" />';
            //console.log(html)
            self.appendHtml(html).hideDialog().focus();
        }
    },'POST',formData,'json');

    4.输入_ajax,查找_ajax名称的函数,在此函数的后面新增如下代码:

    function _ajaxForm(url, fn, method, param, dataType) {
        method = method || 'GET';
        dataType = dataType || 'json';
        var xhr = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
        xhr.open(method, url, true);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4 && xhr.status == 200) {
                if (fn) {
                    var data = _trim(xhr.responseText);
                    if (dataType == 'json') {
                        data = _json(data);
                    }
                    fn(data);
                }
            }
        };
        xhr.send(param);
    }

    5.在此函数后面加上如下代码,这样就可以用ajax方式上传图片了:

    K.ajaxForm=_ajaxForm;

    tip:本文是为了遇到类似问题时方便本人查找

    原文链接:https://blog.csdn.net/alongxiao/java/article/details/104831596

  • 相关阅读:
    redis 源码阅读 数值转字符 longlong2str
    redis 源码阅读 内部数据结构--字符串
    redis 查看的版本
    redis 配置
    redis 基础
    redis 安装
    git 中关于LF 和 CRLF 的问题
    git 常用命令
    linux svn 服务端搭建
    测试开发之Django——No8.Django中的视图与URL配置
  • 原文地址:https://www.cnblogs.com/97pkp/p/12894727.html
Copyright © 2020-2023  润新知