[英]AngularJS not sending data to Node Express
I'm using AngularJS, yes I know AngularJS is outdated and I should be using Angular 9, but this is for a school assignment and I don't really have a choice.我正在使用 AngularJS,是的,我知道 AngularJS 已过时,我应该使用 Angular 9,但这是用于学校作业的,我真的没有选择。
I have a search bar in my html navbar that looks like the following:我的 html 导航栏中有一个搜索栏,如下所示:
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><input ng-model="crypto" type="text" placeholder="Search CryptoDash" style="color: white;"></li>
<li>
<a ng-click="search(crypto)" class="btn white waves-effect waves-light">Submit</a>
</li>
</ul>
In my client.js file, where I store all of the AngularJS app.controller
processing, I have a $scope
function called search.在我的 client.js 文件中,我存储所有 AngularJS
app.controller
处理,我有一个$scope
function 称为搜索。 I transform the input data from the search bar to a JSON blob, and if I console log it, I get {name: "BTC"}
for an example.我将输入数据从搜索栏转换为 JSON blob,如果我在控制台记录它,我会得到
{name: "BTC"}
作为示例。 The function is below: function如下:
// Search the database
$scope.search = function(crypto){
data = {name: crypto};
$http.get('/search', JSON.stringify(data))
.then(function (response) {
console.log("STATUS:", response.status, response.text)
});
};
Now hopping over to my server.js file, I have the skeleton of a basic searching function:现在跳到我的 server.js 文件,我有一个基本搜索 function 的骨架:
// Search route, makes a request to db
app.get('/search', function(req,res){
console.log(req.body);
res.end();
})
The console log on the server side returns undefined
to my console.服务器端的控制台日志将
undefined
返回到我的控制台。 I don't understand why.我不明白为什么。 If I console log just
req
I have a massive log dump hundreds of lines long about headers and sockets etc etc etc, and nowhere req
is my submitted input data.如果我控制台日志只是
req
我有一个巨大的日志转储数百行关于标题和 sockets 等等等等等,并且没有任何地方req
是我提交的输入数据。 I don't know where it went, but one thing is for sure, the /search route is being hit by the front end, so the issue probably lies with how I'm structuring my GET request.我不知道它去了哪里,但有一件事是肯定的, /search 路由正在被前端击中,所以问题可能在于我如何构建我的 GET 请求。 Any help would be appreciated.
任何帮助,将不胜感激。 Thanks.
谢谢。
You need to send data as params
and no need to stringify it:您需要将数据作为
params
发送,无需对其进行字符串化:
$http
.get('/search', { params: data })
.then(function (response) {
console.log("STATUS:", response.status, response.text)
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.