laravel框架+layui做系統時,后台需要使用到富文本編輯器,首選當然簡單的layui自帶的layedit啦,不過上傳圖片時報錯:“請求上傳接口異常”,查看返回 “419 unknown status”,很明顯沒有上傳需要_token值,應該是ajax提交不包含data數據,果斷修改:
layedit.set({ uploadImage:{ url:'/admin/upload', type:'post', data:{ _token:$('meta[name=csrf-token]').attr('content') } } })
並沒有用,查看請求頭,並沒有攜帶token參數,查看文檔,並沒有相應方法,那就只能去修改源碼了。
1. 找到layui-src/dist/layer/modules/layedit.js,復制解壓縮,搜索 uploadImage 找到如下內容
image: function (a) { var n = this; layui.use("upload", function (o) { var r = l.uploadImage || {}; o.render({ url: r.url, method: r.type, elem: e(n).find("input")[0], done: function (e) { 0 == e.code ? (e.data = e.data || {}, v.call(t, "img", { src: e.data.src, alt: e.data.title }, a)) : i.msg(e.msg || "上傳失敗") } }) }) },
修改為
image: function (a) { var n = this; layui.use("upload", function (o) { var r = l.uploadImage || {}; o.render({ url: r.url, data: r.data, method: r.type, elem: e(n).find("input")[0], done: function (e) { 0 == e.code ? (e.data = e.data || {}, v.call(t, "img", { src: e.data.src, alt: e.data.title }, a)) : i.msg(e.msg || "上傳失敗") } }) }) },
2. 將代碼壓縮,而后替換layui-src/dist/layer/modules/layedit.js內容
再測試 OK
