0

It works so far that Angular can make a POST request to my local running Apache server. From that server, I return a Symfony JsonResponse object:

$http.post("/api", {"foo":"bar"})
          .success(function(data, status, headers, config) {
              $scope.data = data;
              console.log(data);
          }).error(function(data, status, headers, config) {
              $scope.status = status;
          });

Now I am stuck with the fact that it only returns []. If I return a hardcoded associative array, it works.

What could be the problem? (plugin)

Richard
  • 4,516
  • 11
  • 60
  • 87

2 Answers2

2

If you are getting [] as data on success, this means your server is giving empty response, thought still a valid response. Try running your API in browser(or better use POSTMAN plugin in Chrome) to check if the same API call is returning [] as a response.

Aniket Sinha
  • 6,001
  • 6
  • 37
  • 50
  • I use it, but I get the same result – Richard May 21 '14 at 16:58
  • That means your server is sending response as []. I dont see any problem with Angular, the problem is on server side. – Aniket Sinha May 22 '14 at 04:59
  • For some reason Apache/php cannot parse the posted variables? that will be my next google search or the plugin is not passing them. Maybe, I can use wireshark to see what is being send? does that work on your own computer? – Richard May 22 '14 at 08:04
1

Ok, the solution was to check for content_type:application/json because the $_POST will not get populated if it is set. Angular automaticly does this.

If you don't know this, it is looking for a needle in a haystack. In that regard, this post was extremely helpfull. and following that I stumbled on this page

this line did it

if(0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/json')){
Community
  • 1
  • 1
Richard
  • 4,516
  • 11
  • 60
  • 87