我正在运行SenseNet 7.0.0
,正在运行它,ASP.NET 5.2.3
并且正在尝试从 Angular (Typescript) 应用程序运行 api 调用。Angular 应用程序运行在localhost:4200
ASP.NET 应用程序上localhost:55064
。我按照本教程安装 Sensenet 并使用本教程安装网页。
当我运行 api 调用时,我收到此错误:
The 'Access-Control-Allow-Origin' header has a value 'http://localhost:55064' that is not equal to the supplied origin. Origin 'http://localhost:4200' is therefore not allowed access.
在 Content Explorer 中,我导航到 Root/System/Settings/Portal.settings。在设置中,我在文件底部添加了下一个代码:
,
AllowedOriginDomains: [ "http://localhost:4200", "http://localhost:55064/" ]
我也试过用[ "*" ]
and[ "localhost" ]
而不是两个本地主机。这是portal.properties 文件的屏幕截图。更改值后,我没有忘记单击保存按钮。我预计这会解决问题,但事实并非如此。即使它不应该涉及重新启动,我也尝试重新启动 asp.net 项目和服务器。那也没有解决问题。我尝试了这些解决方案,因为sensenet wiki和sensenet 文档声明应将外部应用程序的 url 添加AllowedOriginDomains
到白名单中。
当我尝试使用外部程序访问 API 时,如何解决上述错误?
我不认为 Angular 调用是这里的问题,但以防万一:
进口声明:
import {HttpClient} from '@angular/common/http';
HttpClient注入:
constructor(private http: HttpClient) {
}
角度 API 调用:
testApiCall() {
this.http.post(
Configuration.ServerWithApiUrl + '/Odata.svc/(\'Root\')/Login?metadata=no',
'"username" : "admin", "password" : "admin"')
.subscribe(data => this.callResult = data);
}
这是错误的另一次:
The 'Access-Control-Allow-Origin' header has a value 'http://localhost:55064' that is not equal to the supplied origin. Origin 'http://localhost:4200' is therefore not allowed access.
这是从 .asp.net 项目运行的 ajax 调用localhost:55064
。这显示了成功消息。当我从独立的 html 文件运行它时,它还会显示成功消息。当我从独立文件运行它时,它也会显示错误。在错误而不是“localhost:4200”中,它显示“null”。
function testLoginCall() {
$.ajax({
url: "/Odata.svc/('Root')/Login?metadata=no",
dataType: "json",
type: 'POST',
data: JSON.stringify({
'username': "admin",
'password': "admin"
}),
success: function (d) {
console.log('You are logged in!');
}
});
}