2

I am to send a user entered text through Angular js to Node js the issue is the node js server recognizes the event but does not get the data and print it I know I am doing some mistake in the code and do not know how to fix it. can some one please fix this code please.

angular.js

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
   $scope.submit= function(){
      var data = {
          id : "Angkirat"
          Password : "Sandhu"
      }

      $http.post("http://localhost:8081/meow", data)
        .success(function(req,res) {
         console.dir(req.body);
         res.send(req.body);
         alert("hogaya")
       }).error(function() {

      });
   }
});

Server.js

var express = require('express'); 
var app = express();
var bodyParser = require('body-parser');
var cors = require('cors');
var http = require("http");
var fs = require("fs");
var url = require("url");
var display = "";

http.createServer(function(request,response){
    var pathname = url.parse(request.url).pathname;
    console.log("Request for" + pathname + "received.");
    fs.readFile(pathname.substr(1),function (err,data){
        if(err){
            console.log(err);
            response.writeHead(404, {'Content-Type': 'text/html'});
        }else{
            response.writeHead(200, {'Content-Type': 'text/html'});
            response.write(data.toString());
        }
        app.use(cors());
        app.use(bodyParser.json());
        app.post('/meow', function(req, res){
            var cope = req.body.params;
            display = cope;
            console.log(cope);
        });
        response.end();
    });
}).listen(8080);
console.log(display)

Please can some one fix this problem for me.

Mistalis
  • 17,793
  • 13
  • 73
  • 97

5 Answers5

0

You are starting the server in 8080, but your hitting your service from http://localhost:8081/meow

Thalaivar
  • 23,282
  • 5
  • 60
  • 71
0

The server code is in the client code

  //ERRONEOUS
  $http.post("http://localhost:8081/meow", data)
    .success(
      //-------------------------------------
      //THESE LINES BELONG IN THE SERVER CODE
      function(req,res) {
        console.dir(req.body);
        res.send(req.body);
      //-------------------------------------
        alert("hogaya")
   }).error(function() {

  });

The .success method of the $http service has a different signature:

 $http.post(url, data)
   .success(function onSuccess(data, status, headers, config) {
       // Handle success

UPDATE

The deprecated .success and .error methods have been removed from AngularJS 1.6.

Due to b54a39, $http's deprecated custom callback methods - .success() and .error() - have been removed. You can use the standard .then()/.catch() promise methods instead, but note that the method signatures and return values are different.

$http(...)
  .then(function onSuccess(response) {
    // Handle success
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  }).catch(function onError(response) {
    // Handle error
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  });

For more information, see AngularJS Developer Guide - Migrating to v1.6 - http

georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • I tried doing this but I not the error success is not a function so now what do I do? can you please re-type the server code for me please – Angkirat Sandhu Jan 09 '17 at 07:22
  • The `.success` and `.error` functions are deprecated and have been removed from AngularJS 1.6. For more information, see [SO: Why are angular $http success/error methods deprecated? Removed from v1.6?](http://stackoverflow.com/questions/35329384/why-are-angular-http-success-error-methods-deprecated-removed-from-v1-6/35331339#35331339). – georgeawg Jan 09 '17 at 07:45
  • So this code is in the client side right? Since the angular js is there. – Angkirat Sandhu Jan 09 '17 at 09:04
0

In server.js file console req.body instead of req.body.params

    app.post('/meow', function(req, res){
    console.log(req.body);
    console.log(req.body.id) //to access the id
    console.log(req.body.Password) //to access the password
    });
    response.end();
0

Set the following header in your Angular, It should work

.config(function ($httpProvider) {

    $httpProvider.defaults.headers.common = {};
    $httpProvider.defaults.headers.post = { 'Content-Type': 'application/json' };
    $httpProvider.defaults.headers.put = { 'Content-Type': 'application/json' };
    $httpProvider.defaults.headers.patch = {};
})
Ramesh
  • 9
  • 3
0

You are passing the data in the body of the request (in angularJS) but trying to access the data from params (Node.JS)

app.post('/meow', function(req, res){
  var cope = req.body.params;   //Change this line to var cope = req.body
  display = cope;
  console.log(cope);
});
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Vish511
  • 96
  • 7