简体   繁体   English

如何显示基于URL的内容?

[英]How to show content based on the URL?

unfortunately, I have no idea how you call this and had no success finding an answer to my question. 不幸的是,我不知道你怎么称呼它,也没有成功找到我问题的答案。 Basically, I have built a JS application where the user enters a name and the application will then fetch and process data from an API based on that name. 基本上,我构建了一个JS应用程序,用户在其中输入一个名称,然后该应用程序将基于该名称从API中获取并处理数据。

What I want to do now is that if someone goes to http://www.mywebsite.com/NAME , the data will be fetched/processed for NAME. 我现在想做的是,如果有人访问http://www.mywebsite.com/NAME ,则将为NAME提取/处理数据。 I know how it works with PHP (xxx.php?name=NAME) but if possible I don't want to use PHP here. 我知道它如何与PHP(xxx.php?name = NAME)一起使用,但是如果可能的话,我不想在这里使用PHP。

Thank you! 谢谢!

The easiest way to do this is by using URL Rewrite . 最简单的方法是使用URL Rewrite This does not require any coding in JS or server-side language. 这不需要使用JS或服务器端语言进行任何编码。 This can be done on most server softwares but the method is different depending on what server (IIS, Apache, nginx, etc.) you're using. 可以在大多数服务器软件上完成此操作,但是方法因所使用的服务器(IIS,Apache,nginx等)而异。

Here's an article about URL Rewriting to help you get started. 是一篇有关URL重写的文章,可帮助您入门。

For JavaScript you could use the location object. 对于JavaScript,您可以使用location对象。

var url =  window.location.href;

var param = url.replace('http://www.mywebsite.com/', '');

Look at Backbone routers for a more organised way of handling params in JavaScript. 查看骨干路由器,以了解一种更有条理的JavaScript处理参数方法。

http://backbonejs.org/#Router http://backbonejs.org/#Router

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM