zoukankan      html  css  js  c++  java
  • ionic4请求skynet服务器的资源跨域问题

    最近在做一个后台接口,
    顺便用ionic4写了个简单的管理后台,
    本来skynet管理后台监听的端口是6666,
    但是发现chrome默认对一些接口不友善,
    虽然可以通过设置启动参数来解决,
    但是还是把端口改掉了。

    嗯,这个不是今天要记录的内容。

    需要记录的内容是:改了监听端口之后,skynet明明可以响应成功,但是却在前端显示CORS错误。

    之前用nodejs+express的话,很简单的就能解决,利用express的中间件,在响应头里面写入跨域相关的头信息。

    但是skynet的http接口比较晦涩,找起来比较麻烦,skynet的httpd的响应请求部分代码如下:

    local function writeall(writefunc, statuscode, bodyfunc, header)
    	local statusline = string.format("HTTP/1.1 %03d %s
    ", statuscode, http_status_msg[statuscode] or "")
    	writefunc(statusline)
    	if header then
    		for k,v in pairs(header) do
    			if type(v) == "table" then
    				for _,v in ipairs(v) do
    					writefunc(string.format("%s: %s
    ", k,v))
    				end
    			else
    				writefunc(string.format("%s: %s
    ", k,v))
    			end
    		end
    	end
    	local t = type(bodyfunc)
    	if t == "string" then
    		writefunc(string.format("content-length: %d
    
    ", #bodyfunc))
    		writefunc(bodyfunc)
    	elseif t == "function" then
    		writefunc("transfer-encoding: chunked
    ")
    		while true do
    			local s = bodyfunc()
    			if s then
    				if s ~= "" then
    					writefunc(string.format("
    %x
    ", #s))
    					writefunc(s)
    				end
    			else
    				writefunc("
    0
    
    ")
    				break
    			end
    		end
    	else
    		assert(t == "nil")
    		writefunc("
    ")
    	end
    end
    
    function httpd.write_response(...)
    	return pcall(writeall, ...)
    end
    

    所以,我们只需要在响应的时候这样写就可以了

    local headers = {
           ['Access-Control-Allow-Origin'] = '*', -- 这里写允许访问的域名就可以了,允许所有人访问的话就写*
           ['Access-Control-Allow-Credentials'] = true,
        } 
    local ok, err = httpd.write_response(sockethelper.writefunc(id),_,_,headers)
    
  • 相关阅读:
    hdu 4858 项目管理 图的分块
    hdu 3123 GCC 阶乘
    hdu 3065 病毒侵袭持续中 AC自动机
    SPOJ
    hdu 3033 I love sneakers! 分组背包
    zoj 1450 Minimal Circle 最小覆盖圆
    hdu 3007 Buried memory 最远点对
    Azure 虚拟机常见问题-下
    Azure 虚拟机常见问题-上
    关于Windows Azure的常见问题-执行与维护FAQ
  • 原文地址:https://www.cnblogs.com/adoontheway/p/9908438.html
Copyright © 2011-2022 走看看