如何从Express,Param和查询中的URL获取ID似乎不起作用 [英] how to get id from url in express, param and query doesnt seem to work
本文介绍了如何从Express,Param和查询中的URL获取ID似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网址,我正在尝试获取ID,但都无法正常运行req.params或req.query
I have a url, i'm trying to get id but none of it is working req.params nor req.query
app.get('/test/:uid', function testfn(req, res, next) {
debug('uid', req.params.uid); // gives :uid
debug('uid', req.query.uid); // gives undefined
});
我正在像这样进行ajax呼叫
I'm doing an ajax call like this
$(document).on('click', 'a.testlink', function(e) {
$.ajax({
type: "GET",
url: '/test/:uid',
success: function(var) {
console.log('success');
},
error: function() {
alert('Error occured');
}
});
return false;
});
我正在使用
app.use(express.json());
app.use(express.urlencoded());
代替正文解析器
推荐答案
您的代码按预期工作:ajax
调用指定url: '/test/:uid'
,这就是将:uid
放入req.params.uid
的原因.
Your code is working as expected: The ajax
call specifies url: '/test/:uid'
which is what puts :uid
in req.params.uid
.
尝试发送其他内容:url: '/test/123'
和req.params.uid
将包含123
Try sending something else: url: '/test/123'
and req.params.uid
will contain 123
这篇关于如何从Express,Param和查询中的URL获取ID似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文