Angular.js和Node.js获得GET而不是POST

时间:2017-03-30 14:18:52

标签: javascript html angularjs

我想做一个POST,但我仍然在浏览器上有一个GET。我正在使用node和angular。 这是我的HTML格式。

HTML

<form ng-app="formApp" ng-controller="formCtrl">
  nome:<input type="text" ng-model="nome" name="nome"><br>
  cognome:<input type="text" name="cognome"><br>
  ragioneSociale:<input type="text" name="regioneSociale"><br>
  partitaIva:<input type="text" name="partitaIva"><br>
<input type="submit" value="Submit" ng-click="send()">
</form>

这是我的角度函数:

<script>
var fmApp = angualr.module('formApp', []);
fmApp.controller('formCtrl',  function($scope){
  $scope.send = function($http){
    var data = $.params(){
      soggetto: JSON.stringify({
      nome: $scope.nome,
      cognome: $scope.cognome,
      regioneSociale: $scope.regioneSociale,
      partitaIva: $scope.partitaIva
      });
      console.log('ok');
      $http({
        method: "POST",
        url: "/ins/sogg",
        data:'data',
        dataType: 'json'
      }).then(function successCallback(response) {
          console.log("sent")
        }, function errorCallback(response) {
          // called asynchronously if an error occurs
          // or server returns response with an error status.
        });
      // $http.post('/inserimento/soggetto', data).success function(data, status) {
      //   console.log('status');

      })
    };
  };
});

2 个答案:

答案 0 :(得分:2)

您提交的表单未经触及$http来电,因为您不会阻止默认设置。提交按钮的默认操作是提交表单。

更改

<input type="submit" value="Submit" ng-click="send()">

<input type="submit" value="Submit" ng-click="send($event)">

fmApp.controller('formCtrl',  function($scope){
  $scope.send = function($http){

fmApp.controller('formCtrl',  function($scope, $http){
  $scope.send = function(e){
    e.preventDefault();

答案 1 :(得分:0)

<强>&GT;以这种方式解决

  

HTML

    <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>inserimento Dati</title>
  </head>
  <body>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<h1>Inserimento</h1>
<h2>Soggetto</h2>

<form ng-app="formApp" ng-controller="formCtrl">
  nome:<input type="text" ng-model="nome"/><br>
  cognome:<input type="text" ng-model="cognome"/><br>
  ragioneSociale:<input type="text" ng-model="ragioneSociale"/><br>
  partitaIva:<input type="text" ng-model="partitaIva"/><br>
<input type="button" value="Submit" ng-click="send(nome, cognome, ragioneSociale, partitaIva)"/>
  

<强>角

    <script>
    var fmApp = angular.module('formApp', []);
    fmApp.controller('formCtrl',  function($scope, $http){
      $scope.nome,
      $scope.cognome,
      $scope.ragioneSociale,
      $scope.partitaIva,
      $scope.send = function(nome, cognome, ragioneSociale, partitaIva){
        var data = {
            "nome": nome,
            "cognome": cognome,
            "ragioneSociale": ragioneSociale,
            "partitaIva": partitaIva,
          }; // fine JSON
        var str = JSON.stringify(data);
        console.log(data);
        $http({
          method: "POST",
          url: "/ins/sogg",
          data: data,
        }).then(
            function successCallback(response){
              console.log("sent")},
            function errorCallback(response) {
               // called asynchronously if an error occurs
                // or server returns response with an error status.
              });
          // $http.post('/inserimento/soggetto', data).success function(data, status) {
          //   console.log('status');
        };
    });
</script>
  

Server.js

    app.get('/inserimento', function(req, res){
  res.sendFile(__dirname + '/public/inserimento.html');
});

app.post('/ins/sogg', function(req, res){
  var sog = req.body; //arriva il dato
  app.use(bodyParser.json(sog));//dato in json ordinato
  console.log(sog);
  client.connect(function (err){
    if (err) throw err;
  });
  var query = client.query('INSERT INTO "Soggetto" (nome, cognome, "regSociale", "partIVA") VALUES($1, $2, $3, $4)', [sog.nome, sog.cognome, sog.ragioneSociale, sog.partitaIva], function(err, result){
    if(err){
      console.log("Errore");
      return onError(err);
    }
    res.writeHead(200, {'content-type': 'text/plain'});
    res.end('Insert ok');
  });
});