3

I am using $routeProvider to set a route like

when('/grab/:param1/:param2', {
    controller: 'someController',
    templateUrl: "templates/someTemplate.html",
}).

Then in the someController I can read the parameters with $routeParams.param1.

How can I receive a JSON POST instead so I don´t have to use params in the URL ? Is there something I can use in the controller e.g $request.postData ?

EDIT: I'll try to make it more clear

Instead of receiving param1 and param2 (that are in the URL of a GET call) and then use $routeParams to read them and use them in my controller, I would like to receive a JSON object (which of course stays in a POST request) and have that object available in my controller.

stilllife
  • 1,776
  • 1
  • 18
  • 38
  • You mean something like resolve in ui-router? https://github.com/angular-ui/ui-router/wiki – Edminsson Jul 26 '14 at 12:44
  • You need to expand the details of your question. Terms like `receive post` and `don´t use params in the URL` by themselves are very unclear as they don't make sense as simple comments by themselves – charlietfl Jul 26 '14 at 13:00
  • @RahilWazir I meant "receive" a JSON object (which is sent of course as POST) @ Edminsson I guess yes, but I wonder if there is a simple default way in angular without using ui-router – stilllife Jul 26 '14 at 13:04
  • @charlietfl I have edited , maybe it is more clear now – stilllife Jul 26 '14 at 13:08
  • can pass the routeParams to a post request, is that what you are asking? – charlietfl Jul 26 '14 at 13:10
  • I don't get what u mean with "pass". Angular in this scenario is only receiving JSON data, not making a request. Somebody is sending me JSON on that URL and I d like to have the JSON object in the Controller – stilllife Jul 26 '14 at 13:15
  • you can't receive post in client so how can they `send to that url`? You need to explain your full scenario in more detail – charlietfl Jul 26 '14 at 13:20
  • Probably that is the part I am trying to understand. Let's say I do a POST call with a JSON object to mywebsite.com/#/grab . Can angular collect the POST data, redirect to the proper controller, give the JSON to the controller so I can see it ? The POST call is done with a bookmarklet from my browser – stilllife Jul 26 '14 at 13:35
  • If you're passing data around inside the client, will a factory, or something passed to the $rootScope not work? – badAdviceGuy Jul 26 '14 at 13:56
  • I guess I finally got the problem. There is no PHP or whatever server side script on mywebsite.com that can get the JSON object (POST) and JavaScript by it self cannot as well. – stilllife Jul 26 '14 at 15:02
  • Try this http://stackoverflow.com/questions/16930473/angularjs-factory-http-get-json-file – steampowered Jun 12 '15 at 22:37

2 Answers2

0

I finally got the problem. There is no PHP or whatever server side script on mywebsite.com that can get the JSON object (POST) and JavaScript by it self cannot as well. See also this.

Community
  • 1
  • 1
stilllife
  • 1,776
  • 1
  • 18
  • 38
0

You probably want to pass a service to the resolve option of the route in the route provider. The service should implement $http and fetch the json. A resolve option must complete the fetch before the controller initializes. The resolve will also make the json data available in the $scope of the controller.

Setting this up the first time is not trivial, but it is repeatable and also one of the most common ways to pass data from an http request to an Angular controller.

Take a look at this answer: AngularJS: factory $http.get JSON file

Community
  • 1
  • 1
steampowered
  • 11,809
  • 12
  • 78
  • 98