gpt4 book ai didi

angularjs - Angular js : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

转载 作者:行者123 更新时间:2023-12-05 01:12:54 27 4
gpt4 key购买 nike

当我尝试在 Angular Js 中使用 $http.post() 发布数据时出现此错误。

跨源请求被阻止:同源策略不允许读取位于 {{post url}} 的远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。

这是我的代码:

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

ionicAppControllers.config(function($httpProvider) {
//Enable cross domain calls
$httpProvider.defaults.useXDomain = true;

//Remove the header containing XMLHttpRequest used to identify ajax call
//that would prevent CORS from working
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});


ionicAppControllers.controller('createItemCtrl', ['$scope', '$http',
function($scope, $http) {
$scope.parts = [];

$scope.add = function() {
$scope.parts.push({
id: '',
code: '',
description: ''
});
};
$scope.submitItemForm = function(isValid) {
if (isValid) {
$scope.postdata = {};
$scope.postdata.item = $scope.item;
$scope.postdata.description = $scope.description;
for (var i in $scope.parts)
{
for (var j in $scope.parts[i])
{
if (j == '$$hashKey')
{
delete($scope.parts[i][j]);
//console.log(j);
}
}
}
$scope.postdata.parts = $scope.parts;
console.log($scope.postdata);
$http({
method: 'POST',
url: 'URL',
data: $scope.postdata, // pass in data as strings
headers: {'Content-Type': 'application/json'}
}).success(function(data) {
if (data.success)
{
alert('sucess');
}
else
{
alert('fail');
}
});
}
};
$scope.add();
}]);

最佳答案

这与 Angular 无关。服务器应用程序应启用或允许从托管或运行在任何机器上的 Angular 应用程序生成的请求。如果您在本地主机上运行您的应用程序。为 chrome 使用 CORS 扩展。如果它来自其他服务器。让我们说 www.example.com 。

从前端的 Angular 来看,服务器代码必须保持不变。

请引用此链接。 http://enable-cors.org/server.html

关于angularjs - Angular js : Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26650048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com