如何将数据从AngularJS前端传递到Nodejs后端?

如何将数据从AngularJS前端传递到Nodejs后端?,第1张

如何将数据从AngularJS前端传递到Nodejs后端?

它可能是您入门垫脚石

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>
角度代码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-Node.js
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);


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5010308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存