Build URLS from JSON

一曲冷凌霜 提交于 2019-12-24 00:33:22

问题


I've created a function that can build nested urls like so. I was wondering if there existed a more mainstream library to build urls / uris like this. I'd rather use a standard.

utility.urlConstruct({
    "scheme": "https://",
    "domain": "domain.com",
    "path": "/login",
    "query":{
        "user":"thomasreggi",
        "from":utility.urlConstruct({
            "scheme": "https://",
            "domain": "redirect.com",
            "path": "/funstuff",
        }),
    }
});

Spits out

https://domain.com/login?user=thomasreggi&from=https%3A%2F%2Fredirect.com%2Ffunstuff


回答1:


The correct answer is node's built in URL library.

Specifically

url.format(urlObj)



回答2:


jQuery does this internally for its AJAX calls. Perhaps there is a way to access the internal functionality.




回答3:


Al least there are standard conventions:

  • the scheme (or protocol) excludes //
  • your domain is called host (and can include sub-domains or port)
  • path is called pathname
  • query is a list of parameters
  • you might also need a hash

Then it is pretty straightforward, so no real need for a library. You'll find some libraries out there, like jsuri, but their purpose is more to help for parsing than construct (for example to address cross-browser inconsistencies with pathname).




回答4:


I built Scheme.js to build url's via javascript objects, please feel free to offer any advancements.



来源:https://stackoverflow.com/questions/13498417/build-urls-from-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!