2

I'm trying to display a page using a POST route, but so far no luck. The route is called from an Angular controller. The POST route is called properly - I get this in the node console POST /search 200 673ms - but the page is not displayed. If I Invoke the route directly from the browser (i.e. typing the address) it works fine. Any help would be appreciated.

I've found a similar post, but the solution posted there doesn't work for me.

Here's my code:
Angular Controller

function SearchController($scope, $http){
   $scope.product = {...};

   $scope.search = function(form) {
      $http.post('/search', { product : $scope.product });
   };
}

Route definition

  var products = require('../app/controllers/products')
  app.post('/search', auth.requiresLogin, products.results)

Route controller

exports.results = function (req, res) {
   // build query and fields var

   Product.find(query, fields, function (err, docs) {
      res.render('products/results', {
        title: 'Search Results',
        user: req.user ? JSON.stringify(req.user) : "null",
        searchResults: docs ? docs : "null"
      })
   });
}

I'm using Express 3.3.3, Node 0.10.9 and Angular 1.0.3

Community
  • 1
  • 1
dstroppa
  • 23
  • 2

1 Answers1

2

If you are trying to render an express page through your $http call , it will just not work. You should only request data through ajax,not pages.

If you need to redirect to page , use the $window service , in the ajax callback.

$window.location="/search";

if your express controller renders json there should be no problem. but since you are using the method res.render i understand it is not the case.

mpm
  • 20,148
  • 7
  • 50
  • 55
  • 1
    Thanks @mpm, that pointed me in the right direction. I'm now using `res.json` in my `find` method to return json back to the Angular Controller and then to the Jade template – dstroppa Jul 15 '13 at 19:57