[英]MEANJS Get URL Parameters
I'm running my app on localhost:3000/#!/, and trying to get URL parameters for use with Express, with no luck. 我在localhost:3000 /#!/上运行我的应用程序,并尝试获取与Express一起使用的URL参数,但没有成功。 I've created a new server routing file that contains the following:
我创建了一个包含以下内容的新服务器路由文件:
admin.server.routes.js admin.server.routes.js
'use strict';
module.exports = function(app) {
// Admin Routes
var admin = require('../../app/controllers/admin.server.controller');
// Both of these routes work fine.
app.route('/admin/test').
get(admin.populate).
put(admin.update);
// First attempt, didn't work.
app.route('/admin/test').get(admin.doSomething);
// Second attempt, didn't work.
app.param('foo', admin.doSomething);
// Third attempt, didn't work.
app.param('foo', function(req, res) {
console.log('Found foo!');
return res.status(400).send();
});
};
On my admin page, my admin.client.controller.js sends an $http.get request on loading to populate the data. 在我的管理页面上,我的admin.client.controller.js在加载时发送一个$ http.get请求以填充数据。 I have a form with a button the sends an $http.put request to update the populated values.
我有一个带有按钮的表单,该表单发送$ http.put请求以更新填充的值。 Both of these requests work fine.
这两个请求都可以正常工作。
The problem is when I try to visit my app using a URL with the foo parameter, like so: http://localhost:3000/#!/admin/test?foo=bar
. 问题是当我尝试使用带有foo参数的URL访问我的应用程序时,例如:
http://localhost:3000/#!/admin/test?foo=bar
。 I've tried each of the three attempts noted above in my code (commenting out the others out so I could try them one by one), but cannot seem to get the variable. 我已经在代码中尝试了上面提到的三种尝试中的每一种(注释掉其他尝试,以便可以一一尝试),但是似乎无法获取该变量。
In my admin.server.controller file, in addition to the populate and update functions, I simply have this code: 在我的admin.server.controller文件中,除了填充和更新功能外,我还简单地拥有以下代码:
admin.server.controller admin.server.controller
exports.doSomething = function(req, res) {
console.log('Server - found foo!');
};
Using none of these efforts have I actually been able to demonstrate that I've successfully "grabbed" foo for server-side use. 实际上,我没有进行任何努力就可以证明我已经成功“夺取”了foo用于服务器端。 What am I missing?
我想念什么?
In your 在你的
http://localhost:3000/#!/admin/test?foo=bar
all hashbang urls are handled by angularjs, this /admin/test?foo=bar
wouldn't be considered as a request. 所有hashbang网址均由angularjs处理,因此
/admin/test?foo=bar
不会被视为请求。 To add a query string in your request, you can do it like this in angularjs resource: 要在请求中添加查询字符串,可以在angularjs资源中执行以下操作:
function ($resource) {
$resource('/admin/test').query({foo: 'bar'});
}
This would be expressed as this http://localhost:3000/admin/test?foo=bar
这将表示为此
http://localhost:3000/admin/test?foo=bar
Your issue relies mostly on how you send your request on your client-side. 您的问题主要取决于您如何在客户端发送请求。
By the way, in your express routes you can get the foo value like this: Pre-routing with querystrings with Express in Node JS 顺便说一句,在您的快速路由中,您可以获取foo值,如下所示: 在Node JS中使用Express通过查询字符串预先路由
If you wanted to get the query string and use it in your request, refer to this: How can I get query string values in JavaScript? 如果要获取查询字符串并在请求中使用它,请参考此: 如何获取JavaScript中的查询字符串值?
A URL is in the form <protocol>://<hostname>?<query string>#<fragment>
. URL的格式为
<protocol>://<hostname>?<query string>#<fragment>
。
Any part of a URL after (and including) the first # is considered a fragment. URL中位于第一个#之后(包括)的任何部分都被视为一个片段。 Fragments are only used on the client (in this case the browser), and are not typically used by the server.
片段仅在客户端(在本例中为浏览器)上使用,而服务器通常不使用。
From section 3.5 of RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax: 从RFC 3986的 3.5节-统一资源标识符(URI):通用语法:
Fragment identifiers have a special role in information retrieval systems as the primary form of client-side indirect referencing, allowing an author to specifically identify aspects of an existing resource that are only indirectly provided by the resource owner.
片段标识符作为客户端间接引用的主要形式,在信息检索系统中具有特殊的作用,从而允许作者专门标识仅由资源所有者间接提供的现有资源的各个方面。 As such, the fragment identifier is not used in the scheme-specific processing of a URI;
这样,在URI的特定于方案的处理中不使用片段标识符。 instead, the fragment identifier is separated from the rest of the URI prior to a dereference, and thus the identifying information within the fragment itself is dereferenced solely by the user agent, regardless of the URI scheme.
相反,片段标识符在取消引用之前与URI的其余部分分开,因此片段本身内的标识信息仅由用户代理取消引用,而与URI方案无关。
Traditionally fragments are used to link to anchors on the same page. 传统上,片段用于链接到同一页面上的锚点。 A good example is the link provided above to the RFC page, which will jump you down to the section 3.5.
上面提供的RFC页面链接就是一个很好的例子,它将使您跳到3.5节。 Fragment automatically in your browser.
在浏览器中自动分段 。
The URL to the section is https://tools.ietf.org/html/rfc3986#section-3.5
, so the fragment is section-3.5
. 该部分的URL是
https://tools.ietf.org/html/rfc3986#section-3.5
,因此该片段是section-3.5
。 In the HTML for the page there is an anchor within the <h3>
denoting the section with: 在页面的HTML中,
<h3>
有一个锚点,用以下内容表示该部分:
<a class="selflink" name="section-3.5" href="#section-3.5">3.5</a>
The interesting property here is name
, which names the anchor, allowing the browser to identify and jump to it because it matches the value requested by the fragment. 这里有趣的属性是
name
,它为锚定了名称,允许浏览器识别并跳转到该锚,因为它与该片段请求的值匹配。 In effect, the anchor is a client side 'secondary resource' that is being requested by the URI. 实际上,锚点是URI请求的客户端“辅助资源”。
Client side javascript applications and frameworks such as angularjs take advantage of fragments; 客户端javascript应用程序和框架(例如angularjs)利用片段; the fact that they are not for use on the server;
它们不用于服务器的事实; and the fact they are included in the browser history stack;
而事实上,他们包括在浏览器历史堆栈; as a way to reference resources on the client side, for example to use a client side routing system.
作为一种参考客户端资源的方法,例如使用客户端路由系统。
(Aside: #!
has risen as a popular idiom for denoting routes in client side applications. This is possibly related (grammatically but not technically) to the #!
header used in *nix based executable scripts. These are typically followed by a path to an interpreter for the script. For example #!/bin/bash
.) (此外:
#!
已成为表示客户端应用程序中路由的流行用法。这可能(在语法上但从技术上来说)与基于* nix的可执行脚本中使用的#!
标头相关。脚本的解释器,例如#!/bin/bash
。)
So in the URI http://localhost:3000/#!/admin/test?foo=bar
, the segment ?foo=bar
is after the #
denoting the fragment, and is therefore being defined as part of the fragment rather than the query string. 因此,在URI
http://localhost:3000/#!/admin/test?foo=bar
,段?foo=bar
位于#
后面,表示片段,因此被定义为片段的一部分,而不是片段的一部分。请求参数。 A query string for a remote resource should always be defined before the fragment. 始终应在片段之前定义远程资源的查询字符串。 For example:
例如:
http://localhost:3000/admin/test?foo=bar#!/route/in/angularjs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.