我有一个涉及的设置
前端服务器(Node.js,domain:localhost:3000)<--->后端(Django,Ajax,域:localhost:8000)
浏览器< - webapp < - Node.js(服务应用)
浏览器(webapp) - > Ajax - > Django(服务ajax POST请求)
现在,我的问题在于CORS设置,webapp使用它来向后端服务器进行Ajax调用.在chrome中,我一直在努力
当credentials标志为true时,无法在Access-Control-Allow-Origin中使用通配符.
在Firefox上也不起作用.
我的Node.js设置是:
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:8000/');
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
};
Run Code Online (Sandbox Code Playgroud)
webapp发出如下请求:
$.ajax({
type: "POST",
url: 'http://localhost:8000/blah',
data: {},
xhrFields: {
withCredentials: true
},
crossDomain: true,
dataType: 'json',
success: successHandler
});
Run Code Online (Sandbox Code Playgroud)
因此,webapp发送的请求标头如下所示:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept"
Access-Control-Allow-Methods: 'GET,PUT,POST,DELETE'
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: …Run Code Online (Sandbox Code Playgroud) 我Auth0用于我的用户身份验证只允许登录用户访问Spring(Boot)RestController.此时我正在创建一个实时消息功能,用户可以使用和从Angular 2客户端(localhost:4200)发送消息到Spring服务器(localhost:8081).stompjssockjs
尝试创建Stomp客户端并启动连接时,我收到以下控制台错误:
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Run Code Online (Sandbox Code Playgroud)
在研究了这个问题之后,看起来无法同时设置选项origins =*和credentials = true.当我已经将WebSocketConfig中允许的原点设置为客户端域时,如何解决此问题?
Angular 2组件
connect() {
var socket = new SockJS('http://localhost:8081/chat');
this.stompClient = Stomp.over(socket);
this.stompClient.connect({}, function(result) {
console.log('Connected: ' + result); …Run Code Online (Sandbox Code Playgroud) 我正在尝试从不同子域上的 React Native Web 前端对 Django 进行 POST 调用。
我以为我已经正确配置了 CORS,但事实似乎并非如此。
这是我的 Django settings.py 的样子:
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_HEADERS = ['*']
CORS_ALLOWED_ORIGINS = ['https://api.example.com', 'https://example.com', 'https://www.example.com' ]
CSRF_TRUSTED_ORIGINS = [
'https://api.example.com', 'https://example.com', 'https://www.example.com'
]
ALLOWED_HOSTS = ["0.0.0.0", "api.example.com", "example.com"]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
]
INSTALLED_APPS = [
...
'corsheaders',
...
]
Run Code Online (Sandbox Code Playgroud)
我到底做错了什么?我收到的错误是这样的:
Access to XMLHttpRequest at 'https://api.example.com/api/v1/pagescreate/' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the …Run Code Online (Sandbox Code Playgroud)