[英]How to pass multiple parameters in a URL Angular/REST API
I'm trying to pass multiple parameters in a URL with no luck. 我正在尝试在网址中传递多个参数而没有运气。 I'm not sure if it makes a difference but I am doing it through Angular.
我不确定是否会有所作为,但我是通过Angular完成的。 I'm trying to send the request to a REST API backend that I know works for single requests.
我正在尝试将请求发送到我知道适用于单个请求的REST API后端。 Here is what my backend looks like
这是我的后端的样子
index.js index.js
var express = require('express');
var router = express.Router();
var game = require('./game');
router.get('/api/v1/gameRefined/:from_datepicker:to_datepicker:from_timepicker:to_timepicker:selectLevel', game.getAllRefined);
module.exports = router;
game.js game.js
...dbconnection stuff...
var game={
getAllRefined: function(req, res) {
var refines = req.params;
console.log("getting refined games");
pool.getConnection(function(err, connection){
var query = connection.query('SELECT * FROM game WHERE date >= ? AND date <= ? AND time >= ? AND time <= ? and level = ?', [refines.from_datepicker, refines.to_datepicker, refines.from_timepicker, refines.to_timepicker, refines.selectLevel], function(err, rows) {
connection.release();
if(err) {
throw err;
}else{
res.json(rows);
}
});
})
},
}
module.exports = game;
I send the request from this factory 我从这个工厂发出要求
.factory('gameFactory', ['$http',
function($http) {
var _gameFactory = {};
_gameFactory.getRefinedGames = function(dateFrom,dateTo,timeFrom,timeTo,level) {
var encodedParam = encodeURIComponent(baseUrl + '/api/v1/gameRefined/?from_datepicker='+dateFrom+'&to_datepicker='+dateTo+'&from_timepicker='+timeFrom+'&to_timepicker='+timeTo+'&selectLevel='+level+'');
return $http.get(encodedParam);
}
return _gameFactory;
}])
That sends this request that comes back as a 404: http://localhost:8100/http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fv1%2FgameRefined%2F%3Ffrom_datepicker%3D2015-02-05%26to_datepicker%3D2015-02-19%26from_timepicker%3D12%3A00%26to_timepicker%3D18%3A00%26selectLevel%3D1 这将以404的形式发送此请求: http:// localhost:8100 / http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fv1%2FgameRefined%2F%3Ffrom_datepicker%3D2015-02-05%26to_datepicker%3D2015-02 -19%26from_timepicker%3D12%3A00%26to_timepicker%3D18%3A00%26selectLevel%3D1
I have tried it encoded and not encoded, with forward slashs, with semi colons, but nothing has worked so far. 我已经尝试过使用正斜杠,半冒号对它进行编码和不编码,但是到目前为止没有任何效果。 I don't know why a localhost gets appended at the start, but even trying it in postman without the first localhost still is a 404 error.
我不知道为什么在开始时会附加一个本地主机,但是即使在没有第一个本地主机的邮递员中尝试它也仍然是404错误。
How should I be doing this? 我应该怎么做? Any help is appreciated.
任何帮助表示赞赏。 Thank you.
谢谢。
First, other than separating the work into a factory, you aren't really using Angular here. 首先,除了将工作分成工厂之外,您实际上并没有在这里使用Angular。 Second, I would consider posting to your API, return JSON results from the API, and then use a promise to read the data back into Angular.
其次,我将考虑发布到您的API,从API返回JSON结果,然后使用Promise将数据读回到Angular。
Let Angular do this work for you. 让Angular为您完成这项工作。 Something like the below will generate the URL request for you.
如下所示的内容将为您生成URL请求。 The return value of $http is also a promise , so using .success and .error will allow you to parse any returned data as well, even if it is just a success or failure message - but it is a great method of passing data between server/API and client.
$ http的返回值也是一个保证 ,因此使用.success和.error还将允许您解析任何返回的数据,即使它只是成功或失败的消息,但这也是在之间传递数据的好方法服务器/ API和客户端。
.factory('gameFactory', ['$http',
function($http) {
return {
reachAPI: function(dateFrom, dateTo) {
$http.post('http://localhost:8080/api/v1', {
'dateFrom': dateFrom,
'dateTo': dateTo
})
.success(function(data, status, headers, config) {
*things happen*
data.somethingReturned // from your API
});
}
}]);
I know you said you have confidence in your REST API structure, but body-parser is an Express middleware that can parse URL-encoded strings and may prove helpful in reading your data. 我知道您说过您对REST API结构充满信心,但是body-parser是一种Express中间件,可以解析URL编码的字符串,并可能有助于读取数据。 Personally, I think it lends towards more readable code.
就我个人而言,我认为它有助于提高可读性。
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
router.route('/api').post(function(req, res) {
*things happen*
req.body.dateFrom //find your data
*things happen*
res.json(returnedData);
});
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.