我想从angular中保存一些字段,我有以下代码:
<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>
在web 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请求失败)。
发布于 2015-10-12 15:56:55
你能帮我查一下你的航班前。检查正常或失败,即带有谓词“options”的请求失败,错误为403。在尝试将数据从angular应用程序发布到web api时,我遇到了类似的问题
https://stackoverflow.com/questions/33055726
复制相似问题