0

My index.html file in ionic looks as follows...

<ion-content>
  <form name="login" method="post" action="/">
    <div class="list list-inset">
    <label class="item item-input">
      <input type="text" placeholder="First Name">
    </label>
    <label class="item item-input">
      <input type="text" placeholder="Last Name">
    </label>
      <input type="submit" value="Submit">
  </div>
</form>
</ion-content>

Its a simple form, that's all. But when submit is clicked I want to be able to catch the request using node.js.

I have a separate server file with the following in it...

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

app.post('/', function (req, res) {
    console.log("post worked");
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

In Ionic, in my angular app.js I am trying to connect to this node.js server file...

angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {

      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
});

.factory('Session', function ($resource) {
  return $resource('http://XXX.XXX.X.XX:3000/sessions/:sessionId');
});

But I do not think the connect is working. I'm using my private IP to connect. How can I get this to work? Also, how do I know if node.js is picking up the request? Where will console.log("post worked") print to?

EDIT

I changed my angular module to Mini Bhati's answer...

angular.module('starter',['ionic','ngResource']);

but I still cannot get the POST request

1
  • Where is your / router where you want to listening in server side? Commented Sep 17, 2016 at 3:11

1 Answer 1

1

Add the dependency injection on the ngResource module for using $resource in your main app module (app.js in your case) as Angular does not come bundled with the $resource service.

angular.module('starter',['ionic','ngResource']);

Also, please make sure the script angular-resource.js is included in your HTML page.

Sign up to request clarification or add additional context in comments.

2 Comments

This solved the ngResource error, but I still can't get the post to work. Any idea why?
The sessionId needs to be passed in the $resource service as well. Like this $resource('http://XXX.XXX.X.XX:3000/sessions/:sessionId',{ sessionId: @_sessionId' });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.