ArtitalkJS / Artitalk

通过leancloud实现的可实时发布说说的js
https://artitalk.js.org
MIT License
329 stars 52 forks source link

按文档配置一直出跨域问题 #111

Open lvfeiyang111 opened 2 years ago

lvfeiyang111 commented 2 years ago

请按照本模板编写issues

描述 一直下雨界面,然后控制台报跨域

出现bug的页面 页面链接: image 重现 请重现你遇到bug的过程

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

截图 如果方便的话,在这里添加截图来更清晰的说明你遇到的bug。

桌面设备 (please complete the following information):

移动设备 (please complete the following information):

其他内容 在此处添加其他信息

612901 commented 1 year ago

如若出现跨域问题,请绑定自定义域名,如若绑定自定义域名还是出现跨域问题,将结构化数据中的shuoshuo列表权限全部改成所有用户

youngjuning commented 1 year ago

垃圾 leancloud

PeaceJiang commented 1 year ago

如若出现跨域问题,请绑定自定义域名,如若绑定自定义域名还是出现跨域问题,将结构化数据中的shuoshuo列表权限全部改成所有用户

好像也没有解决

SinMu-L commented 1 month ago

用这个CF worker 试试

/**
 * Welcome to Cloudflare Workers! This is your first worker.
 *
 * - Run "npm run dev" in your terminal to start a development server
 * - Open a browser tab at http://localhost:8787/ to see your worker in action
 * - Run "npm run deploy" to publish your worker
 *
 * Learn more at https://developers.cloudflare.com/workers/
 */
function rp(p) {
  return p.split("?")[0]
}
function l(p) {
  return p[getJsonLength(p) - 1]
}

function remove_protocol(domain){
  return domain.slice(8)
}

function getJsonLength(jsonData) {

  var jsonLength = 0;

  for (var item in jsonData) {

    jsonLength++;

  }

  return jsonLength;
}

export default {
  async fetch(request, env, ctx) {
    var AppId = (function () { try { return env.APPID } catch (e) { return "" } })()
    var AppKey = (function () { try { return env.APPKEY } catch (e) { return "" } })()
    if (AppId == "" || AppKey == "") { return new Response('Artitalk-Safe异常:您没有设定appid和appkey') }
    var ServerDomain = (function () { try { return env.SERVERDOMAIN } catch (e) { return `${(AppId.substr(0, 8)).toLowerCase()}.api.lncldglobal.com` } })()
    var atComment = (function () { try { return env.ATCOMMENT == "true" ? true : false } catch (e) { return true } })()
    var CORS = (function () { try { return env.CORS } catch (e) { return '*' } })()
    console.log(request.url,"===")
    const url = new URL(request.url)

    const urlObj = new URL(url)
    const path = urlObj.href.substr(urlObj.origin.length)
    const classac = l(rp(path).split('/'))
    if (classac == "atComment" && !atComment) { return new Response('{"code":101,"error":"Artitalk-Safe:评论功能未开启"}', { headers: { "content-type": "application/json;charset=utf-8" } }) }
    if (classac == "_File" || classac == "_Followee" || classac == "_Follower" || classac == "_Installation" || classac == "_Role") { return new Response('{"code":101,"error":"Artitalk-Safe:操作是禁止的"}', { headers: { "content-type": "application/json;charset=utf-8" } }) }

    var hostname = remove_protocol(ServerDomain)
    url.hostname = hostname
    console.log(url,"+++");

    var reqHEDNew = new Headers(request.headers)
    if (reqHEDNew.get("X-LC-Id")) {
      reqHEDNew.set("X-LC-Id", AppId)
      reqHEDNew.set("X-LC-Key", AppKey)
    }
    if (reqHEDNew.get("x-lc-sign")) {
      reqHEDNew.delete("X-LC-Sign")
    }

    var reqNew = new Request(request, { headers: reqHEDNew })
    var responsefetch = await fetch(url.toString(), reqNew)

    var resHEDNew = new Headers(responsefetch.headers)
    resHEDNew.set("Access-Control-Allow-Origin", CORS)
    resHEDNew.set("test-header", "9.99")
    var response = new Response(responsefetch.body, { headers: resHEDNew })
    return response
  },
};

workers 里面的环境变量配置如下

SERVERDOMAIN 变量以 https:// 开头

image

补充:我这边LearnCLoud 的配置里面配置了自定义域名