找到文件 :/usr/lib/lua/luci/dispatcher.lua 的 一下代码处:
if track.cors and http.getenv("REQUEST_METHOD") == "OPTIONS" then //如果是复杂请求
luci.http.status(200, "OK")
luci.http.header("Access-Control-Allow-Origin", http.getenv("HTTP_ORIGIN") or "*")
luci.http.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
return
end
以上代码是开启复杂请求的跨域模式, 但是默认并没有开启,因为track.cors值默认未赋值, 开启只需要将track.cors 赋值为true即可, 如下:
track.cors = true //开启复杂请求的跨域
if track.cors and http.getenv("REQUEST_METHOD") == "OPTIONS" then //如果是复杂请求
luci.http.status(200, "OK")
luci.http.header("Access-Control-Allow-Origin", http.getenv("HTTP_ORIGIN") or "*")
luci.http.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
return
end
如果不涉及到复杂请求, 只是简单请求的话, 可以改为一下代码,同时开启简单请求和复杂请求
track.cors = true //开启请求的跨域
luci.http.header("Access-Control-Allow-Origin", http.getenv("HTTP_ORIGIN") or "*")
luci.http.header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
if track.cors and http.getenv("REQUEST_METHOD") == "OPTIONS" then //如果是复杂请求
luci.http.status(200, "OK")
return
end
网友评论