2017-12-11 8 views
3

이 질문을하기 전에이 내용을 읽었습니다. related post.XMLHttpRequest에 의해 시작된 요청의 자격 증명 모드는 withCredentials 특성에 의해 제어됩니다.

INSTALLED_APPS = [ 
    ... 
    'corsheaders', 
] 

CORS_ORIGIN_ALLOW_ALL = False 
CORS_ALLOW_CREDENTIALS = True 
CORS_ORIGIN_WHITELIST = (
     'http://103.200.30.76' 
     ) 

내 웹 사이트의 프론트 엔드는 아파치가 80 수신 포트 사용, 그리고 내가

python3 manage.py runserver 103.200.30.76:8001 

를 사용하지만 여전히 울부 짖는 오류 얻을 :

Failed to load http://103.200.30.76:8001/api/website/websitemanage/footerreconmend/list/ : Response to preflight request doesn't pass access control check: 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://103.200.30.76 ' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

을 내 settings.py에서

요청 중 하나는 다음과 같습니다.

General: 

Request URL:http://103.200.30.76:8001/api/website/websitemanage/homepagefunctionshow/list/ 
Request Method:OPTIONS 
Status Code:200 OK 
Remote Address:103.200.30.76:8001 
Referrer Policy:no-referrer-when-downgrade 

Response Headers 

Access-Control-Allow-Credentials:true 
Access-Control-Allow-Headers:accept, accept-encoding, authorization, content-type, dnt, origin, user-agent, x-csrftoken, x-requested-with 
Access-Control-Allow-Methods:DELETE, GET, OPTIONS, PATCH, POST, PUT 
Access-Control-Allow-Origin:http://103.200.30.76 
Access-Control-Max-Age:86400 
Content-Length:0 
Content-Type:text/html; charset=utf-8 
Date:Mon, 11 Dec 2017 02:44:12 GMT 
Server:WSGIServer/0.2 CPython/3.5.2 
Vary:Origin 
X-Frame-Options:SAMEORIGIN 

Request Headers: 

Accept:*/* 
Accept-Encoding:gzip, deflate 
Accept-Language:zh-CN,zh;q=0.9,en;q=0.8 
Access-Control-Request-Headers:access-control-allow-origin,x-requested-with 
Access-Control-Request-Method:GET 
Connection:keep-alive 
Host:103.200.30.76:8001 
Origin:http://103.200.30.76 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36 

누가 도와 드릴 수 있습니까?


편집

내가 크롬을 열 노호 명령 (disable-web-security)를 사용하는 경우 나, 찾기, 그 문제가되지 않습니다.

class AccessControl(MiddlewareMixin): 
    def process_request(self, request): 

     if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META: 
      response = http.HttpResponse() 
      response["Access-Control-Allow-Origin"]= "*" 
      response["Access-Control-Allow-Credentials"] = "true" 
      response["Access-Control-Allow-Methods"]= "GET,HEAD,OPTIONS,POST,PUT" 
      response["Access-Control-Allow-Headers"] = "Authentication , Authorization , X-CSRF-Token , Access-Control-Allow-Credentials , Access-Control-Allow-Methods , Access-Control-Allow-Origin , Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers" 

      return response 

     return None 

하지만 여전히이 문제를 가지고 : -


open -a "Google Chrome" --args --disable-web-security --user-data-dir

편집

이 나는 ​​중간 제품을 사용 Naqib Hakimi의 답변을 시도했다.

enter image description here

가 request.META에는 HTTP_ACCESS_CONTROL_REQUEST_METHOD 없습니다 :

나는 디버거에서 요청을 확인.

답변

0

기본적으로 django는 모든 도메인에 대해 Access-Control-Allow-Origin을 허용하지 않으므로 MIDDLEWARE_CLASSES를 추가해야합니다.

class AccessControl(object): 
    def process_request(self, request): 

     if 'HTTP_ACCESS_CONTROL_REQUEST_METHOD' in request.META: 
      response = http.HttpResponse() 
      response["Access-Control-Allow-Origin"]= "*" 
      response["Access-Control-Allow-Credentials"] = "true" 
      response["Access-Control-Allow-Methods"]= "GET,HEAD,OPTIONS,POST,PUT" 
      response["Access-Control-Allow-Headers"] = "Authentication , Authorization , X-CSRF-Token , Access-Control-Allow-Credentials , Access-Control-Allow-Methods , Access-Control-Allow-Origin , Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers" 

      return response 

     return None 

다음

MIDDLEWARE_CLASSES = [ 
    ... 
    'app.filename.AccessControl', 

    ] 

setting.py

이 모든 도메인

+0

이 요청은 나를 위해 작동하지 않을 수 있습니다. 나는 그것을 시도했다. – fanhualuojin154873