问题
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)
回答1:
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.
回答2:
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')){
来源:https://stackoverflow.com/questions/23784675/post-requests-from-grunt-connect-proxy-plugin-do-not-contain-data