angularJS sending OPTIONS instead of POST

前端 未结 4 935
一个人的身影
一个人的身影 2021-01-18 08:57

Im stuck at this 2 days I can not find a solution. When im doing an AngularJS POST it Sends OPTIONS in the header and returns error from the API the code looks like this no

4条回答
  •  爱一瞬间的悲伤
    2021-01-18 09:43

    Should only need to do this code to get it to work:

    angular.module('TestApp', [])
       .factory('someService', ['$http', someService]);
    
    function someService() {
      var service = {
        save: save
      };
    
      var serviceUrl = '/some/Url';
    
      return service;
    
      function save(data) {
        $http.post(serviceUrl, data)
              .success(function(data, status, headers, config) {
                  alert(data);
              })
              .error(function(data, status, headers, config) {
                  console.log("Error");
              });
      }
    }
    

    Then pull your someService into your controller and use:

    someService.save(data);
    

提交回复
热议问题