Rohit Hazra
Rohit Hazra

Reputation: 667

Simple Angular Post request returning error

First of all i have seen a lot of resources and tried them but with no luck.

I am trying to get some data from node server by posting two variables.

Note: I would prefer to not use JQuery if Possible

I think the problem is here:

var data = {
        "username": "trumpt",
        "offset": "0"
      };

here is a fiddle http://jsfiddle.net/pa21anen/

Edit: I am getting the output as {"errors":"Invalid Input"} but it should be this with the same url and data and request type. I have successfully implemented it in android, ios, windows 10 apps

{
  "notifications": [
    {
      "id": 1,
      "sender": "trumpt",
      "title": "test 1",
      "body": "Hello,\nFor all of those who use neo4j as a database along side with node.js, I have created a service that mediates any listing, finding or filtering request between an api and a neo4j database. The communication is done over rest api. \nThis service alows you to find/list any node from within your neo4j database using related nodes properties (from any relationship distance) \nIt was build as light as possible and it's only purpose is to generate appropriate cypher queries based on given filters and not intens processing\nIt is very easy to deploy and use.\nIssues, Pull requests and Enhancement requests are very welcomed and encouraged ! grin emoticonHello,\nFor all of those who use neo4j as a database along side with node.js, I have created a service that mediates any listing, finding or filtering request between an api and a neo4j database. The communication is done over rest api. \nThis service alows you to find/list any node from within your neo4j database using related nodes properties (from any relationship distance) \nIt was build as light as possible and it's only purpose is to generate appropriate cypher queries based on given filters and not intens processing\nIt is very easy to deploy and use.\nIssues, Pull requests and Enhancement requests are very welcomed and encouraged ! grin emoticonHello,\nFor all of those who use neo4j as a database along side with node.js, I have created a service that mediates any listing, finding or filtering request between an api and a neo4j database. The communication is done over rest api. \nThis service alows you to find/list any node from within your neo4j database using related nodes properties (from any relationship distance) \nIt was build as light as possible and it's only purpose is to generate appropriate cypher queries based on given filters and not intens processing\nIt is very easy to deploy and use.\nIssues, Pull requests and Enhancement requests are very welcomed and encouraged ! grin emoticon\nHello,\nFor all of those who use neo4j as a database along side with node.js, I have created a service that mediates any listing, finding or filtering request between an api and a neo4j database. The communication is done over rest api. \nThis service alows you to find/list any node from within your neo4j database using related nodes properties (from any relationship distance) \nIt was build as light as possible and it's only purpose is to generate appropriate cypher queries based on given filters and not intens processing\nIt is very easy to deploy and use.\nIssues, Pull requests and Enhancement requests are very welcomed and encouraged ! grin emoticon",
      "priority": 3,
      "time": 1455503708,
      "type": 1,
      "attachments": "image458286.jpg,pdf-sample.pdf,sample.doc,SampleGrades.xls"
    },
    {
      "id": 2,
      "sender": "trumpt",
      "title": "test 2",
      "body": "another test notif",
      "priority": 1,
      "time": 1455474927,
      "type": 1,
      "attachments": "oimage458286.jpg"
    },
    {
      "id": 3,
      "sender": "trumpt alter",
      "title": "test by new user",
      "body": "just a frickin' test",
      "priority": 2,
      "time": 1455478746,
      "type": 1,
      "attachments": null
    }
  ]
}

Upvotes: 0

Views: 39

Answers (1)

JanisP
JanisP

Reputation: 1243

This is the way, how you can serialize data without JQuery for POSTing to server in AngularJS.

.controller('myCtrl', function($scope, $http, $httpParamSerializerJQLike) {
    $scope.SendData = function() {

      var data = {
        "username": "trumpt",
        "offset": "0"
      };

      $http({
          method: 'POST',
          url: 'https://trumpt-nigharsh.c9users.io/notifications/getAll',
          data: $httpParamSerializerJQLike(data),
          headers: {'Content-Type': 'application/x-www-form-urlencoded'}

      }).success(function(data, status, headers, config) {
          $scope.PostDataResponse = data;
        })
        .error(function(data, status, header, config) {
          $scope.PostDataResponse = data
        });
    };

Working sample here: https://plnkr.co/edit/Wp3dj6FBIq09V3tbDPV0?p=preview

NB! this serialization was introduced only in some latter AngularJS builds. Tested on 1.4.8 Edit: Short version for POST: https://plnkr.co/edit/EGXIBJV24H0u7QO8JYEX?p=preview

Upvotes: 1

Related Questions