在离子中处理CROS

时间:2016-10-20 08:43:26

标签: rest ionic-framework

我试图在我的离子应用程序中使用远程api ..但它显示以下错误

XMLHttpRequest cannot load http://localhost:54276/api/products?key1=value1&key2=value2. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.

正在使用chromeionic serve上运行... 我在离子博客上阅读了一些关于CROS的文章,但我没有把它弄好。任何人帮我解决这个问题都是我的controller代码

$scope.getData = function() { $http.get("http://localhost:54276/api/products", { params: { "key1": "value1", "key2": "value2" } }) .success(function(data) { $scope.username = data.username; $scope.password = data.password; }) .error(function(data) { alert("Sorry"); }); }

2 个答案:

答案 0 :(得分:0)

向您发送标头http请求

var request = {
 method: 'GET',
 url: 'http://localhost:54276/api/products',
 headers: {
   'Access-Control-Allow-Origin': '*'
 },
 params: { "key1": "value1", "key2": "value2" }
}

$http(request).then(function(){...}, function(){...});

希望这对你有所帮助。感谢

答案 1 :(得分:0)

感谢每一个......只需添加

即可
 "proxies": [{
    "path": "/api",
    "proxyUrl": "//your url"
}]

ionic.config