Sim*_*mer 6 javascript node.js cors angularjs sails.js
我有一个Angular 1.x应用程序,该应用程序在Sails.js应用程序中调用API。每当我尝试从Angular应用中调用API时,都会得到-
XMLHttpRequest cannot load http://localhost:1337/portal/login. Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains the invalid value ''. Origin 'http://localhost:8080' is therefore not allowed access.
由于我的Sails.js应用程序还有很多其他API不会在此Angular应用程序上使用,因此我不想通过allRoutes: true
在config / cors.js中进行设置将CORS应用于所有这些API。因此,我遵循了Sails.js的文档,并以这种方式编写了自定义CORS配置-
'/portal/login': {
target: 'MyController.login',
cors: {
origin: '*',
credentials: true,
methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD, PATCH',
headers: 'content-type, Authorization'
}
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用。如果启用,allRoutes: true
则它开始工作,但我不想在所有路由上启用CORS并公开它们。我已经尝试过所有可能的组合,origin, credentials, methods, headers
但始终会给出相同的错误。
您能帮我解决这个问题吗?提前谢谢。
小智 -2
Sails.js
您可以通过在 config 文件夹内的安全文件中启用 CORS 来做到这一点:
config/security.js
Run Code Online (Sandbox Code Playgroud)
这是它的代码:
cors: {
allRoutes: true,
allowOrigins: '*',
allowCredentials: false,
}
Run Code Online (Sandbox Code Playgroud)
Sails.js
您可以在官方文档
中查看它的参考: https://sailsjs.com/documentation/concepts/security/cors
归档时间: |
|
查看次数: |
510 次 |
最近记录: |