简体   繁体   English

如何从Angular JS中的$ http服务发送JSON数据到Web API

[英]How to send json data to web api from $http service in angular js

While making a Ajax Request with Json object to " Web Api " using Angular js $http service.It raise CORS Issue,There is no Resolution by adding the following header " Access-Control-Allow-Origin " also,Please suggest me the right solution 当使用Angular js $ http服务通过Json对象向“ Web Api ”发出Ajax请求时,会引发CORS问题,通过添加以下标头“ Access-Control-Allow-Origin ”也没有解决方法,请向我建议正确的方法解

Example: 例:

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) { })
}
})

The "Access-Control-Allow-Origin" is a server side issue, you don't need to add any header in angularjs. “ Access-Control-Allow-Origin”是服务器端的问题,您无需在angularjs中添加任何标头。 add the following code to your server side: 将以下代码添加到服务器端:

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

you can refer to How to enable CORS in AngularJs 您可以参考如何在AngularJs中启用CORS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM