我正在本地主机上运行我的应用程序:3000/#!/,并试图获得与Express一起使用的URL参数,但没有成功。我创建了一个新的服务器路由文件,其中包含以下内容:
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();
});
};在我的管理页面上,我的admin.client.controller.js在加载时发送一个$http.get请求来填充数据。我有一个带有按钮的表单,它发送一个$http.put请求来更新已填充的值。这两个请求都很好。
问题是当我尝试使用带有foo参数的URL访问我的应用程序时,如:http://localhost:3000/#!/admin/test?foo=bar.:我已经尝试了我的代码中提到的三次尝试中的每一次(注释掉了其他尝试,以便我可以一个一个地尝试它们),但似乎无法得到变量。
在我的admin.server.controller文件中,除了填充和更新函数之外,我还有以下代码:
admin.server.controller
exports.doSomething = function(req, res) {
console.log('Server - found foo!');
};使用这些努力中的任何一个,我实际上都能够证明我已经成功地“抓取”了foo以供服务器端使用。我遗漏了什么?
发布于 2014-12-10 16:15:47
在您的
http://localhost:3000/#!/admin/test?foo=bar所有的hashbang urls都由angularjs处理,这个/admin/test?foo=bar不会被视为请求。要在请求中添加查询字符串,可以在angularjs资源中这样做:
function ($resource) {
$resource('/admin/test').query({foo: 'bar'});
}这将表示为这个http://localhost:3000/admin/test?foo=bar
你的问题主要取决于你如何在客户端发送你的请求。
顺便说一下,在您的特快路线中,您可以得到如下所示的foo值:Pre-routing with querystrings with Express in Node JS
如果您想获取查询字符串并在请求中使用它,请参考以下内容:How can I get query string values in JavaScript?
https://stackoverflow.com/questions/27404935
复制相似问题