POST requests from grunt-connect-proxy plugin do not contain data

巧了我就是萌 提交于 2019-12-25 06:31:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!