从AngularJS客户端发送POST表单数据到Express / Node.js服务器 [英] Sending POST form data from AngularJS client to Express/Node.js server

查看:115
本文介绍了从AngularJS客户端发送POST表单数据到Express / Node.js服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从AngularJS表单读取数据并将其发送到Express服务器。我的客户端功能发送数据确实执行,但请求永远不会到达服务器。我相信网址有问题。

I am trying to read data from an AngularJS form and send it to an Express server. My client function to send the data does execute, but the request never reaches the server. I believe there is an issue with the URLs.

我的 AngularJS控制器的相关部分

$scope.loginUser = function() {
   $scope.statusMsg = 'Sending data to server...';
   $http({
      url: 'http://##.##.##.##/login.js', // IP address replaced with ##'s
      method: 'POST',
      data: user,
      headers: {'Content-Type': 'application/json'}
})

这个执行,因为我看到消息从第一行显示。 url 是我的Express服务器的绝对路径。这个URL应该是什么? (是否需要指向一些现有文件?)

This does execute, because I see the message appear from the first line. The url is the absolute path of my Express server. What should this URL be? (Does it need to point to some existing file?)

我的 Express服务器的相关部分 login.js

Relevant part of my Express server (login.js in the same directory):

var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.post('*', function(req, res) {
   console.log('Processing request...');
   res.sendFile('/success.html');
});

app.listen(3000);

我执行这个服务器,它应该在端口3000上监听。但是当我的客户端执行时,上面的 app.post 方法从不运行,因为我从来没有看到处理请求... 输出在控制台。这里的路径应该是什么?是否需要同意客户端的URL?

I execute this server, and it listens on port 3000 as it should. But when my client executes, the app.post method above never runs, because I never see the Processing request... output on the console. What should the path here be? Does it need to agree with the URL in the client?

这里有什么问题?我相信整体结构是可以的, strong>路径/ URL应该是什么?(即不仅仅是他们的上述情况的值,而是其语义,在线文档并没有清楚)。

What is the issue here? I believe the overall structure is OK, but what should the paths/URLs be? (i.e., not just their values for the case above, but their semantics. The online documentation didn't make this clear.)

如果有帮助,我的HTML表单使用 method =post,而不指定操作。我已经调整了这些和许多其他设置,没有运气。

If it helps, my HTML form uses method="post" and no action specified. I have adjusted these and many other settings with no luck.

推荐答案

答案是列出的答案的组合。 >

The answer is a combination of the answers listed.


  1. 不要 列出快速服务器在URL中的扩展名。

  2. IP地址之后,将端口号包含为:3000

  1. Do not list the extension of the Express server in the URL.
  2. Include port number as :3000 immediately following the IP address.

AngularJS客户端

url: 'http://##.##.##.##:3000/login'

Express服务器路由

 app.post('/login', function(req, res) {
     console.log('Processing request...');
     res.sendFile('/success.html');
 });

我还发现,我的Amazon Web Services实例的安全组阻止了端口3000上的入站流量我的应用程序在我打开这个港口到外面的世界之后工作。

I also found out that the security group for my Amazon Web Services instance was blocking incoming traffic on port 3000. My application worked after I opened this port to the outside world.

这篇关于从AngularJS客户端发送POST表单数据到Express / Node.js服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆