我正在尝试使用以下方法获取项目运行的URL:
const url = window.location.origin;
它有效,但我面临的问题是任何子目录都没有被选中。因此,当我为https://my-app.com/ui
运行时,只会选择https://my-app.com
。
https://my-app.com
指向https://my-app.com/ui
,这也是我的应用运行的位置。
我能想到的唯一解决方案是拆分window.location.pathname
但当原点未使用任何子目录时会中断,例如http:localhost:4200
或https://dev.myApp.com
。
如何修改上面的代码以包含子目录,以便它适用于所有上述场景?
修改
我在Angular 5上,所以也许有一种Angular方式?
给定网址的预期输出:
https://myApp.com/ui/dashboard/events -> https://myApp.com/ui
http://localhost:4200 -> http://localhost:4200
https://sites.dev.myApp.com/ui2/dashboard/events -> https://sites.dev.myApp.com/ui2
https://sites.dev.myApp.com/ui2/app/dashboard/events -> shttps://ites.dev.myApp.com/ui2/app/dashboard/events
答案 0 :(得分:0)
只需从origin
/
pathname
之前的var output = location.origin + ( location.pathname ? location.pathname.split( "/" )[0] : "" )
和第一个值
import requests
import execjs
url = 'http://www.sgx.com/JsonRead/JsonstData?qryId=RAll'
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
page = requests.get(url,headers=headers)
content = page.content[len('{}&& '):] if page.content.startswith('{}&& ') else page.content
data = execjs.get().eval(content)
print(data)