是否有通过查询字符串传递数组的标准方法?
为了清楚起见,我有一个包含多个值的查询字符串,其中一个值是数组值.我希望将查询字符串值视为数组 - 我不希望数组被展开,以便它与其他查询字符串变量无法区分.
此外,根据这篇帖子的答案,作者建议不定义对数组的查询字符串支持.这准确吗?
编辑:
基于@Alex的回答,没有标准的方法可以做到这一点,所以我的后续工作是什么是一种简单的方法来识别我正在阅读的参数是PHP和Javascript中的数组?
将多个参数命名为同名是否可以接受,这样我知道它们属于一个数组?例:
?myarray=value1&myarray=value2&myarray=value3...
Run Code Online (Sandbox Code Playgroud)
或者这是不好的做法?
我有一个javascript对象,我需要展平成一个字符串,以便我可以作为查询字符串传递,我该怎么做?即:
{ cost: 12345, insertBy: 'testUser' } 会成为 cost=12345&insertBy=testUser
我不能使用jQuery AJAX调用这个调用,我知道我们可以使用它并传递对象,data但不是在这种情况下.然而,使用jQuery来展平对象是可以的.
谢谢.
我正在提出这样的请求:
fetch("https://api.parse.com/1/users", {
method: "GET",
headers: headers,
body: body
})
Run Code Online (Sandbox Code Playgroud)
如何传递查询字符串参数?我只是将它们添加到URL吗?我在文档中找不到一个例子.
我想知道有没有办法从客户端向后端API get Request传递一个参数。此时,我对所需的参数进行了硬编码(名称:“newName”)。
后端路由:
app.get('/api/get/beerWithComments', (req,res,next) =>{
Beer.findOne({name:'newName'}) //I want to pass the correct name, now it's hard coded.
.populate('comments').exec((err,beer) =>{
if(err) return next(err);
res.json(beer);
});
});
Run Code Online (Sandbox Code Playgroud)
我的行动方法:
export const fetchBeerWithComments =() => async dispatch => {
const res= await axios.get('/api/get/beerWithComments');
dispatch({type: FETCH_BEER_WITH_COMMENTS, payload : res.data });
}
Run Code Online (Sandbox Code Playgroud)
我想将参数传递到这里。但我不知道是否可以将参数传递到我的后端。
export const fetchBeerWithComments =(parameter) => async dispatch => {...
Run Code Online (Sandbox Code Playgroud) 我有一个 React 组件,它必须对 MongoDB 数据库执行带有两个参数的 find({}) 查询。
const likes = await Likes.find({ postId: postId, userId: userId }).exec()
Run Code Online (Sandbox Code Playgroud)
由于 Mongo 代码只能在服务器上运行,所以我必须进行 API 调用(我正在使用 NextJS)。这个API调用显然是一个GET请求。如何使用 SWR(或 fetch)将“postId”和“userId”传递给获取请求?
我试图将它们作为对象通过“身体”传递,但我认为这根本不是正确的方法。
const likesPerUser = {
postId: postId,
userId: userId
}
const docs = await fetch('/api/likes/user', {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(likesPerUser),
})
Run Code Online (Sandbox Code Playgroud)
我无权访问 URL 查询字符串
我有一种感觉,我可能有点跑题了。任何帮助将非常感激。干杯,马特
node.js ×2
reactjs ×2
arrays ×1
express ×1
fetch ×1
flatten ×1
javascript ×1
jquery ×1
mongodb ×1
mongoose ×1
next.js ×1
php ×1
query-string ×1
react-native ×1
swr ×1
url ×1
url-encoding ×1