angularJS发送选项,而不是POST的 [英] angularJS sending OPTIONS instead of POST

查看:185
本文介绍了angularJS发送选项,而不是POST的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林停留在这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);

这篇关于angularJS发送选项,而不是POST的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆