angular $resource POST produces an invalid JSON bo

2019-08-12 17:50发布

问题:

The following code sends a POST request without a body (an empty body) when running in IE 10, in Chrome and Firefox, it behaves correctly,

angular.module('test', ['ngResource', 'ngRoute']).
  config([function() {}])

angular.module('test').
  controller('c1', ['$scope', '$http', '$resource', function($scope, $http, $resource) {

      $scope.zaza = "popo"

      var Supplyers = $resource('api/supplyer', null,
          {fetch: { method: 'POST', isArray: true}}
      )

      var ids = [1,2,3]

      Supplyers.fetch(
        {ids: ids},
         function(suppliers) {
            console.log(">>>" + suppliers)
         },
         function(err) {
            console.log("!!!" + err)
         }
      )

  }])

Angular's documentation gives info about IE versions older than 9 :

http://docs.angularjs.org/guide/ie

but does not mention issues with ie 10...

Version of angular is :

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular-resource.min.js" type="text/javascript"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2/angular-route.min.js" type="text/javascript"></script>

======= update

I tested the code in "IE9 mode" and also in "IE8 mode" and and surprizingly enough, they both work, so the problem exists only in IE 10...

======= update

I included the html, note the change in angular version (1.0.8) it also behaves the same way (post has empty body ...)

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <!--[if lte IE 8]>
    <script src="/assets/javascripts/lib/json3.min.js"></script>
    <![endif]-->


    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular-resource.js" type="text/javascript"></script>


    <!-- https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js
    https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.2
    -->

    <script src="/assets/javascripts/test.js" type="text/javascript"></script>
</head>
<body ng-app="test" xmlns:ng="http://angularjs.org" id="ng-app">

    <div ng-controller="c1">

        <h1>{{zaza}}</h1>

    </div>


</body>
</html>

Note: a post with $http.post has the same problems :

  $http.post('api/supplyer', {ids: ids}).success(function(res){
    console.log("<<" + res)
  }).error(function(res){
    console.log("!!"+ res)
  })

回答1:

I hit the same problem as you . In my case, using angular.toJson() produced a valid body.