1

I'm writing an application which uses AngularJS 1.4.0 which requires the ability to receive POST data from an external application. I know that routes in AngularJS often do parameters in the URL such as this:

.when('/section/:param', {
    templateUrl: 'views/home.html',
    controller: 'AppCtrl'
})

The problem with this style is that in my case the parameter is often very very long and it will cause the web server to ignore / truncate the request / parameter because the URL exceeds the maximum URL length.

For this reason, instead of a standard GET style parameter, I would like the application to receive a POST parameter, but I am not sure how to capture the parameter(s) and value(s).

Is there a way for Angular to capture the POST parameters directly? My second option would be to simply have an ng-init which uses a backend to grab the values, but I'd prefer to keep the parameters solely in Angular if possible. Thanks!

kkirsche
  • 1,217
  • 2
  • 15
  • 38
  • have a look here: https://docs.angularjs.org/api/ng/service/$http – m4lt3 Jun 08 '15 at 13:13
  • Thanks @m4lt3, sadly $http, as I understand it, is only for sending AND receiving data. I'm only looking to receive POST data from an external application. – kkirsche Jun 08 '15 at 13:16
  • then this might help: https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage – m4lt3 Jun 08 '15 at 13:21

1 Answers1

2

Except if your willing to do some weird black magic by setting cookies server-side - or something similar - there is no way to this in javascript.

POST values are sent to the server upon request, it's impossible capture these with javascript running in your browser.

Check out this answer aswell: How to read the post request parameters using javascript

Community
  • 1
  • 1
Hless
  • 3,326
  • 20
  • 22
  • That's what I was afraid of. Thanks for the clarification though. I'll catch it on the server then and send it in an ng-init into the app / JS. Thanks! – kkirsche Jun 08 '15 at 13:25
  • 1
    Ah, okay that's what you meant with the ng-init part. Sound like a decent solution imo. I would probably use a factory to inject the variables though, ng-init feels kinda dirty. Matter of preference though. – Hless Jun 08 '15 at 13:28
  • Matter of fact, here is the docs on ng-init: https://docs.angularjs.org/api/ng/directive/ngInit . They seem to discourage the use of that directive except for one specific use case. – Hless Jun 08 '15 at 13:30
  • Thanks @Hless. I guess I won't use ng-init. I'll have to look at factories more and determine how best to get the params from the backend into the factory – kkirsche Jun 08 '15 at 13:35