gpt4 book ai didi

javascript - 我收到 "No ' Access-Control-Allow-Origin' header 错误“即使我设置了它

转载 作者:行者123 更新时间:2023-11-29 17:56:54 25 4
gpt4 key购买 nike

<分区>

即使我在 angular 的 $http 服务中分配了 header ,我仍然遇到此错误:

function Hello($scope, $http) {
$http({
method: 'GET',
url: 'http://localhost:8080/races',
headers: {
'Access-Control-Allow-Headers': 'accept',
'Access-Control-Allow-Methods': 'GET',
'Access-Control-Allow-Origin': '*'
},
}).then(function successCallback(response) {
$scope.races = response;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});

这是控制台上的完整错误消息:

XMLHttpRequest cannot load http://localhost:8080/races. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63343' is therefore not allowed access. The response had HTTP status code 403.

HTML:

<!doctype html>
<html ng-app>
<head>
<title>Hello AngularJS</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script src="hello.js"></script>
</head>

<body>
<div ng-controller="Hello">
<p>The ID is {{races.raceId}}</p>
<p>The name is {{races.raceName}}</p>
</div>
</body>
</html>

当我打开 localhost:8080/races 时,我可以完全看到 json:

[{"raceId":8446,"raceName":"test1"},{"raceId":8447,"raceName":"test2"}]

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