Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lvfeiyang111 opened this issue Nov 6, 2022 · 4 comments
Open

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

lvfeiyang111 opened this issue Nov 6, 2022 · 4 comments

Comments

@lvfeiyang111
Copy link

请按照本模板编写issues

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

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

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

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

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

  • 操作系统: windows
  • 浏览器 chrome,
  • 版本 [e.g. 22]

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

  • 型号: [e.g. iPhone6]
  • 操作系统: [e.g. iOS8.1]
  • 浏览器 [e.g. stock browser, safari]
  • 版本 [e.g. 22]

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

@612901
Copy link

612901 commented Jan 10, 2023

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

@youngjuning
Copy link

垃圾 leancloud

@PeaceJiang
Copy link

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

好像也没有解决

@SinMu-L
Copy link

SinMu-L commented Oct 6, 2024

用这个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 的配置里面配置了自定义域名

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants