不应用跨源资源共享设置
Cross-origin resource sharing setting isn't applied
我正在 运行宁 SenseNet 7.0.0
,运行宁它与 ASP.NET 5.2.3
我正在尝试 运行 一个 api从 Angular (Typescript) 应用程序调用。 Angular 应用程序 运行 在 localhost:4200
上,ASP.NET 应用程序 运行 在 localhost:55064
上。我按照 this tutorial for installing Sensenet and used this tutorial 安装网页。
当我 运行 一个 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.
在内容资源管理器中,我导航到 Root/System/Settings/Portal.settings。在设置中,我将下一个代码添加到文件底部:
,
AllowedOriginDomains: [ "http://localhost:4200", "http://localhost:55064/" ]
我也尝试过使用 [ "*" ]
和 [ "localhost" ]
而不是两个本地主机。 Here is a screenshot of the portal.properties file. I didn't forget to click the save button after changing the value. I expected this would fix the issue, but it didn't. Even though it should not involve a restart, I tried restarting the asp.net project and the server. That didn't resolve the problem either. I tried these solution because the sensenet wiki and the sensenet docs 声明 url 的外部应用程序应添加到 AllowedOriginDomains
以将它们列入白名单。
如何修复上述错误,这是我在尝试使用外部程序访问 API 时遇到的错误?
我认为 Angular 调用不是这里的问题,但以防万一:
导入语句:
import {HttpClient} from '@angular/common/http';
HttpClient注入:
constructor(private http: HttpClient) {
}
Angular 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.
这是 ajax 调用 运行 来自 localhost:55064
上的 asp.net 项目。这显示了成功消息。 当我从一个独立的 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!');
}
});
}
将 "localhost"
添加到设置文件中允许的来源列表, 没有协议和端口 。
我认为您的请求与 headers 或方法(POST、GET、PUT 等)不匹配
试试这个或类似的会议方式。
[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]
正如 Miklós 提到的,其中一个站点的 url 应该与本地主机不同。我做了一个快速测试,它在没有 https 的情况下工作,所以步骤如下:
- 转到 IIS 管理器并创建一个新站点。将您的 sensenet 安装的 web 文件夹的路径添加为物理路径,并添加除 localhost 以外的主机名。
将您的自定义主机名添加到您的主机文件 (\Windows\System32\drivers\etc\hosts)
打开 sensenet 的管理界面(又名内容资源管理器)并将这个新的 url 添加到 Default_Site 的 url 列表(您可以修改列表如果您编辑网站内容)
通过上述这些步骤,我设法修复了 CORS 错误。
原来这是Sensenet 7.0.0的bug或限制。可以看到状态here。
目前,解决方法是 build the Angular project 使用 ng build --base-href=~/Scripts/Angular
并将 dist
文件夹的内容粘贴到 ASP.NET 的 /Scripts/Angular
文件夹中项目。然后,将 _Layout.cshtml
文件的内容替换为 dist
文件夹中 index.html 文件的内容,将 @RenderBody()
放回 _Layout.cshtml
和 运行 ASP.NET 项目
这两个 api 调用现在都可以使用解决方法:
testLoginApiCall() {
this.http.post(
Configuration.ServerWithApiUrl + '/Odata.svc/(\'Root\')/Login?metadata=no',
'{"username" : "admin", "password" : "admin"}')
.subscribe(
data => console.log('Succes!', data),
error => console.log('An error occured.', error));
}
testCreateWorkspaceCall() {
this.http.post(
Configuration.ServerWithApiUrl + 'OData.svc/(\'Root\')',
'models=[{"__ContentType": "Workspace", "DisplayName": "Workspace"}]')
.subscribe(
data => console.log('Succes!', data),
error => console.log('An error occured.', error));
}
我正在 运行宁 SenseNet 7.0.0
,运行宁它与 ASP.NET 5.2.3
我正在尝试 运行 一个 api从 Angular (Typescript) 应用程序调用。 Angular 应用程序 运行 在 localhost:4200
上,ASP.NET 应用程序 运行 在 localhost:55064
上。我按照 this tutorial for installing Sensenet and used this tutorial 安装网页。
当我 运行 一个 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.
在内容资源管理器中,我导航到 Root/System/Settings/Portal.settings。在设置中,我将下一个代码添加到文件底部:
,
AllowedOriginDomains: [ "http://localhost:4200", "http://localhost:55064/" ]
我也尝试过使用 [ "*" ]
和 [ "localhost" ]
而不是两个本地主机。 Here is a screenshot of the portal.properties file. I didn't forget to click the save button after changing the value. I expected this would fix the issue, but it didn't. Even though it should not involve a restart, I tried restarting the asp.net project and the server. That didn't resolve the problem either. I tried these solution because the sensenet wiki and the sensenet docs 声明 url 的外部应用程序应添加到 AllowedOriginDomains
以将它们列入白名单。
如何修复上述错误,这是我在尝试使用外部程序访问 API 时遇到的错误?
我认为 Angular 调用不是这里的问题,但以防万一:
导入语句:
import {HttpClient} from '@angular/common/http';
HttpClient注入:
constructor(private http: HttpClient) {
}
Angular 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.
这是 ajax 调用 运行 来自 localhost:55064
上的 asp.net 项目。这显示了成功消息。 当我从一个独立的 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!');
}
});
}
将 "localhost"
添加到设置文件中允许的来源列表, 没有协议和端口 。
我认为您的请求与 headers 或方法(POST、GET、PUT 等)不匹配
试试这个或类似的会议方式。
[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]
正如 Miklós 提到的,其中一个站点的 url 应该与本地主机不同。我做了一个快速测试,它在没有 https 的情况下工作,所以步骤如下:
- 转到 IIS 管理器并创建一个新站点。将您的 sensenet 安装的 web 文件夹的路径添加为物理路径,并添加除 localhost 以外的主机名。
将您的自定义主机名添加到您的主机文件 (\Windows\System32\drivers\etc\hosts)
打开 sensenet 的管理界面(又名内容资源管理器)并将这个新的 url 添加到 Default_Site 的 url 列表(您可以修改列表如果您编辑网站内容)
通过上述这些步骤,我设法修复了 CORS 错误。
原来这是Sensenet 7.0.0的bug或限制。可以看到状态here。
目前,解决方法是 build the Angular project 使用 ng build --base-href=~/Scripts/Angular
并将 dist
文件夹的内容粘贴到 ASP.NET 的 /Scripts/Angular
文件夹中项目。然后,将 _Layout.cshtml
文件的内容替换为 dist
文件夹中 index.html 文件的内容,将 @RenderBody()
放回 _Layout.cshtml
和 运行 ASP.NET 项目
这两个 api 调用现在都可以使用解决方法:
testLoginApiCall() {
this.http.post(
Configuration.ServerWithApiUrl + '/Odata.svc/(\'Root\')/Login?metadata=no',
'{"username" : "admin", "password" : "admin"}')
.subscribe(
data => console.log('Succes!', data),
error => console.log('An error occured.', error));
}
testCreateWorkspaceCall() {
this.http.post(
Configuration.ServerWithApiUrl + 'OData.svc/(\'Root\')',
'models=[{"__ContentType": "Workspace", "DisplayName": "Workspace"}]')
.subscribe(
data => console.log('Succes!', data),
error => console.log('An error occured.', error));
}