简体   繁体   English

将数据从 angular 发送到在不同域上运行的 express nodejs 服务器

[英]Sending data from angular to express nodejs server running on different domain

So I am trying to send some data I have in angular to my nodejs server.所以我试图将一些我有角度的数据发送到我的 nodejs 服务器。 Let me start with my code: index.html让我从我的代码开始:index.html

 <body ng-app="starter"> <ion-pane> <ion-header-bar class="bar-stable"> <h1 class="title">Ionic Blank Starter</h1> </ion-header-bar> <ion-content> </ion-content> </ion-pane> <div ng-controller="myCtrl"> <form> <input type="text" ng-model="message"> <input type="submit" value="Submit" ng-click="submit()"> </form> </div> </body>

Here is a text box with data, then the submit button will send the message and deviceID to the server.这是一个带有数据的文本框,然后提交按钮会将消息和设备ID 发送到服务器。

app.js应用程序.js

 var app = angular.module('starter', []); app.controller('myCtrl', function($scope, $http) { var devToken; var push = new Ionic.Push({ "debug": true }); push.register(function(token) { devToken = token; console.log("Device token:", token.token); }); $scope.submit = function() { var message = "Hello"; $http({ method: 'POST', url: 'http://127.0.0.1:8080', token: devToken, message: message }) } })

As you can see, I am trying to grab a message and the deviceID and post it to my express server.如您所见,我正在尝试获取消息和设备 ID 并将其发布到我的快速服务器。

server.js服务器.js

 var express = require('express'); var bodyParser = require('body-parser'); var apns = require('apns'); var gcm = require('node-gcm'); var app = express(); var sender = new gcm.Sender('AIzaS.........plD2s8'); var registrationTokens = []; registrationTokens.push('Insert Device ID here that is passed from app.js'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.get('/', function(req, res) { console.log("DEVICE ID == " + req.body.devToken); sender.send("Hello", { registrationTokens: registrationTokens }); }) app.listen(8080, function() { console.log('running on port 8080'); });

SO to recap, I am needing help getting my data when submitted to be available on my nodejs express server (which runs at a different domain/port than the Angular app).总结一下,我需要帮助在提交数据时获取我的数据,以便在我的 nodejs express 服务器(它在与 Angular 应用程序不同的域/端口上运行)上可用。 Can anyone help me figure out how to get my data from angular to express nodejs?谁能帮我弄清楚如何从 angular 获取我的数据来表达 nodejs? Any help is appreciated.任何帮助表示赞赏。

thanks谢谢

You need to create a route to capture the post:您需要创建一条路线来捕获帖子:

app.post('/', function(req, res) {
    console.log(req.body);
    ...
});

Then the front end could pass whatever you want然后前端可以通过任何你想要的

    $http.post("/", {message:message, devToken:devToken}).success(function(message, status) {
        console.log('Message posted');
    })

If the backend is not on the same origin as the OP states in the comments, you would need to enable cross origin requests.如果后端与注释中的 OP 状态不在同一源上,则您需要启用跨源请求。

app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});

Note: don't use * for production unless really want to allow requests from anywhere.注意:除非真的想允许来自任何地方的请求,否则不要将 * 用于生产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM