【问题标题】:How to send json data to web api from $http service in angular js如何从 Angular js 中的 $http 服务将 json 数据发送到 web api
【发布时间】:2016-03-09 20:53:32
【问题描述】:

使用 Angular js $http 服务向“Web Api”发出带有 Json 对象的 Ajax 请求时。它会引发 CORS 问题,通过添加以下标头“Access-”无法解决Control-Allow-Origin”,请建议我正确的解决方案

例子:

var testmodule = angular.module('testmod', []);

 testmodule.controller('testcontroller', function ($scope, $http) 

{

$scope.Member ={'name':'andrew','age':12,'class':10}

$scope.savedata =function(){

$http({      
            url: "**api url**",
            method: 'POST',
            data: $scope.Member
        }).success(function (data, status) { }).error(function (data, status, headers) { })
}
})

【问题讨论】:

  • 请在此处输入一些代码...

标签: angularjs ajax asp.net-web-api


【解决方案1】:

“Access-Control-Allow-Origin”是服务器端问题,您不需要在 angularjs 中添加任何标头。将以下代码添加到您的服务器端:

("Access-Control-Allow-Origin", "*");   
("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");         
("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia");

可以参考How to enable CORS in AngularJs

猜你喜欢
  • 2016-06-12
  • 2013-08-07
  • 1970-01-01
  • 2015-10-05
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多