“NetworkError:使用web api从angular.js调用post方法时不允许405方法

时间:2015-10-10 15:39:25

标签: angularjs asp.net-mvc asp.net-web-api2 asp.net-web-api

我想从角度保存som字段 我有以下代码:

<script>
    angular.module('ProductAdd', [])
    .controller('ProductAddController', ['$scope', '$http', function ($scope, $http) {
        $scope.submit = function () {
            if ($scope.Nume) {
                var product = {
                    "Nume": $scope.Nume,
                    "Prenume": $scope.Prenume,
                    "CNP": $scope.CNP,
                    "Telefon": $scope.Telefon
                }
                console.log(product);
                $http.post('http://localhost:1110/api/product', product).
                success(function (data, status, headers, config) {
                    alert('Product Added Successfully');
                }).
                error(function (data, status, headers, config) {
                    console.log(config, status);
                    alert("error");
                });
            }
        };
    }]);

</script>

在网络APi中我有以下控制器 :

public class ProductController : ApiController
{

    public static Lazy<List<Pacient>> products = new Lazy<List<Pacient>>();//Static variable use only for demo, don’t use unless until require in project. 
    public static int PgaeLoadFlag = 1; // Page load count. 
    public static int ProductID = 4;

    // GET api/product
    public List<Pacient> GetAllProducts() //get method
    {
        //Instedd of static variable you can use database resource to get the data and return to API
        return products.Value; //return all the product list data
    }

    // GET api/product/5
    public IHttpActionResult GetProduct(int id)
    {
        Pacient product = products.Value.FirstOrDefault((p) => p.PacientID == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

   // POST api/product
    public void ProductAdd(Pacient product) //post method
    {
        product.PacientID = ProductID;
        products.Value.Add(product); //add the post product data to the product list
        ProductID++;
        //instead of adding product data to the static product list you can save data to the database.
    }

但是当我想要保存产品时,

“NetworkError:405方法不允许 - http://localhost:1110/api/product

还有2个警告:

阻止跨源请求:同源策略禁止在http://localhost:1110/api/product读取远程资源。 (原因:CORS预检频道没有成功)。 跨源请求已阻止:同源策略禁止在http://localhost:1110/api/product读取远程资源。 (原因:CORS请求失败)。

1 个答案:

答案 0 :(得分:0)

你能检查一下你的预飞行吗?检查没问题或失败我是一个请求动词&#39;选项&#39;失败并出现错误403.我在尝试将数据从角应用程序发布到web api

时遇到了类似的问题