美文网首页
ueditor-跨域问题

ueditor-跨域问题

作者: YomonAh | 来源:发表于2017-07-31 15:25 被阅读0次

    项目开发中用了百度ueditor编辑器,在用到ueditor的表情时,遇到了跨域问题,因为我们项目中所有的js、css、图片等静态资源都是放在cdn里,跟项目工程不在同个服务器,所以导致了跨域问题。
    举个例子示意下:

    项目域名:http://home.index.html,对应index.css、 index.js
    表情相关资源域名:http://cdn.emotion.html,对应emotion.css、emotion.js
    http://cdn.emotion.html是以iframe的形式嵌在http://home.index.html下,在读取emotion.html下的资源的时候就报跨域的错误了
    

    去网上查了下iframe跨域解决方案,千篇一律说:

    在index.html里加  document.domain = "home.com";//根域
    在emotion.html里加  document.domain = "home.com";//根域
    

    但是用这种方式解决跨域问题是有条件限制的,你只能赋成当前的域名或者基础域名,亲测确实不行,还会报错,所以就放弃了这个方案

    我的解决方案是找到ueditor的配置文件ueditor.config.js里的iframeUrlMap,重新配置表情对应iframe的src:

    iframeUrlMap:{
      'emotion:'http://home.emotion.html'
    }
    

    然后把ueditor->dialogs->emotion下的emotion.html到项目工程下,更改里面js、css等资源的请求路径,重新部署后,ifarme里的src就会由原先的“http://cdn.emotion.html”变成“http://home.emotion.html”,这样就不涉及跨域问题了。

    同理,对于ueditor的超链接iframe导致的跨域问题,也可以通过这个方法解决:

    iframeUrlMap:{
      'emotion:'http://home.emotion.html',
      'link':'http://home.link.html'
    }
    

    再将ueditor下的link.html移植到工程下,一起发布到服务器,就不会产生跨域问题。

    相关文章

      网友评论

          本文标题:ueditor-跨域问题

          本文链接:https://www.haomeiwen.com/subject/orkrlxtx.html