如何将数据从AngularJS前端传递到Nodejs后端? [英] How to pass data from AngularJS frontend to Nodejs backend?

查看:265
本文介绍了如何将数据从AngularJS前端传递到Nodejs后端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 <!DOCTYPE html> 
< html lang =en>

< head>
< script src =Scripts / angular.min.js>< / script>
< body>
< div ng-app =>
< form>
作者:
< input type =textng-model =author>
< br>
< br>标题:
< input type =textng-model =title>
< br>
< br>正文:
< input type =authorng-model =body>
< br>
< br>
< input type =submitvalue =提交>
< / form>
< / div>
< / head>
< / body>
< / html>

以及带有MySQL代码的节点js。我能够从这个节点代码将数据传递给MySQL DB。如何继续角度到节点js?我来自PHP背景。我应该能够从角度js形式发送数据到MySQL数据库。我可以从节点代码发送数据到MySQL数据库。

  var mysql = require('mysql'); 

var connection = mysql.createConnection({
host:'localhost',
user:'',
password:'',
database: 'copedb'
});
connection.connect();
var cope = {
author:'XYZXYZ',
title:'Testing Node',
body:'Node JS'
};
var query = connection.query('insert into cope set?',cope,function(err,result){
if(err){
console.error(err);
return;
}
console.error(result);
});


解决方案

它可以成为您入门的垫脚石:

Index.html



 <!DOCTYPE html> 
< html lang =en>
< head>
< script src =Scripts / angular.min.js>< / script>
< script src =Scripts / app.js>< / script>
< / head>
< body ng-app =myApp>
< div ng-controller =myCtrl>
< form>
作者:
< input type =textng-model =author>
< br>
< br>标题:
< input type =textng-model =title>
< br>
< br>正文:
< input type =authorng-model =body>
< br>
< br>
< input type =submitvalue =Submitng-click =submit()>
< / form>
< / div>
< / body>



角码app.js



  var app = angular.module('myApp',[]); 
app.controller('myCtrl',function($ scope){
$ scope.submit = function(){
var data = $ .param({
book:JSON .stringify(
author:$ scope.author,
title:$ scope.title,
body:$ scope.body
})
});

$ http.post(/ api / book /,data).success(函数(数据,状态){
console.log('已成功发布数据');
})
}
});



server.js - Nodejs



  var express = require('express'); 
var mysql = require('mysql');
var app = express();

var connection = mysql.createConnection({
host:'localhost',
user:'',
password:'',
database: 'copedb'
});
connection.connect();
$ b app.post('/ api / book',function(req,res,next){
var cope = req.body.params;
var query = connection。查询('insert into cope set?',cope,function(err,result){
if(err){
console.error(err);
return res.send(err) ;
} else {
return res.send('Ok');
}
});
app.listen(8080);


I have some angular js code here.

<!DOCTYPE html>
<html lang="en">

  <head>
    <script src="Scripts/angular.min.js"></script>
        <body>    
      <div ng-app="">
        <form>
          Author:
          <input type="text" ng-model="author">
          <br>
          <br> Title:
          <input type="text" ng-model="title">
          <br>
          <br> Body:
          <input type="author" ng-model="body">
          <br>
          <br>
          <input type="submit" value="Submit">
        </form>
      </div>
    </head>
    </body>
</html>

and node js with MySQL code here. I am able to pass data to MySQL DB from this node code. How to go ahead with angular to node js? I am coming from a PHP background. I should be able to send data from angular js form to MySQL database. I am able to send data to MySQL database from node code here.

var mysql = require('mysql');

var connection = mysql.createConnection({
  host: 'localhost',
  user: '',
  password: '',
  database: 'copedb'
});
connection.connect();
var cope = {
  author: 'XYZXYZ',
  title: 'Testing Node',
  body: 'Node JS'
};
var query = connection.query('insert into cope set ?', cope, function(err, result) {
  if (err) {
    console.error(err);
    return;
  }
  console.error(result);
});

解决方案

It can be the stepping stone for you to getting started:

Index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <script src="Scripts/angular.min.js"></script>
   <script src="Scripts/app.js"></script>
</head>
<body ng-app="myApp">    
  <div ng-controller="myCtrl">
    <form>
      Author:
      <input type="text" ng-model="author">
      <br>
      <br> Title:
      <input type="text" ng-model="title">
      <br>
      <br> Body:
      <input type="author" ng-model="body">
      <br>
      <br>
      <input type="submit" value="Submit" ng-click="submit()">
    </form>
  </div>
</body>

Angular code app.js

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.submit= function(){
      var data = $.param({
        book: JSON.stringify({
            author: $scope.author,
            title : $scope.title,
            body : $scope.body
        })
      });

      $http.post("/api/book/", data).success(function(data, status) {
        console.log('Data posted successfully');
      })
   }
});

server.js - Nodejs

var express = require('express'); 
var mysql = require('mysql');
var app = express();

var connection = mysql.createConnection({
     host: 'localhost',
     user: '',
     password: '',
     database: 'copedb'
});
connection.connect();

app.post('/api/book', function(req, res, next){
   var cope = req.body.params;
   var query = connection.query('insert into cope set ?', cope, function(err, result) {
     if (err) {
       console.error(err);
       return res.send(err);
     } else {
       return res.send('Ok');
     }
});
app.listen(8080);

这篇关于如何将数据从AngularJS前端传递到Nodejs后端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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