angularJS发送选项,而不是POST的而不是、选项、angularJS、POST

2023-09-14 00:28:18 作者:眼前荒芜

林停留在这2个日子里,我无法找到一个解决方案。当IM做一个AngularJS POST它发送的头选项,并从API返回错误code看起来像这样没什么特别的。

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 nothing special.

$http.defaults.headers.post["Content-Type"] = "application/json";
$http.post(URL, JSON.stringify(data)).
    success(function(data, status, headers, config) {
        alert(data);
    error(function(data, status, headers, config) {
        console.log("Error");
});

CORS是在它有头的API,当我做了POST,但在Chrome小提琴手或邮递员只当我使用angularJS后也不会去直通工作正常启用。

CORS is enabled on the API it has the Headers, when i do POST with fiddler or POSTMan in Chrome it works fine only when i use angularJS post it won't go thru.

为什么我得到选项/ SubmitTicket HTTP / 1.1,而不是POST?

我需要做的,张贴的内容?我看了一下它,它说像CORS是添加选项头,但为什么呢?

What do i need to do to POST ? I have read about it it says something like CORS is adding OPTIONS header but why?

推荐答案

应该只需要做到这一点code,以得到它的工作:

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");
          });
  }
}

然后拉你的 someService 到控制器及用途:

someService.save(data);