2017-07-24 3 views
0

XMLHttpRequest ne peut pas charger http://host1:5000/endpoint. La réponse à la demande de contrôle en amont ne passe pas la vérification du contrôle d'accès: aucun en-tête «Access-Control-Allow-Origin» n'est présent sur la ressource demandée. L'origine 'http://originhost:3000' n'est donc pas autorisée.

Comment résoudre ce problème en utilisant anguarjs de face ou côté serveur (script CGI Python). Je n'ai pas le droit de changer quoi que ce soit avec un navigateur, crome ou sécurité et même pas possible avec demande.

angulaire code frontal:

function scheduleReportServiceFactory($resource){ 
 
\t \t var schedule_report_url = 'http://host1/endpoint:bucket'; 
 
\t \t var auth = btoa("xyz:xyz"); 
 
\t \t var headers = { 
 
\t \t \t "Authorization" : "Basic " + auth, 
 
       'Content-Type': 'application/json', 
 
       'Access-Control-Allow-Origin': 'http://originhost:3000' 
 
\t \t }; 
 
\t \t return { 
 
\t \t \t schedule_report : $resource(schedule_report_url, {}, { 
 
\t \t \t \t query : { 
 
\t \t \t \t \t method : 'GET', 
 
\t \t \t \t \t params : { 
 
\t \t \t \t \t \t bucket : '@bucket' 
 
\t \t \t \t \t }, 
 
\t \t \t \t \t headers: headers, 
 
\t \t \t \t \t cache : true, 
 
\t \t \t \t } 
 
\t \t \t }) 
 
\t \t }; 
 
\t 
 
\t }
backend code Python:
import ..... 
 

 

 
class Dispatcher(object): 
 

 
    def not_found(self, environ, start_response): 
 
     start_response('404 Not Found', [('Content-Type', 'application/octet-stream')]) 
 
     return [] 
 

 
    def __init__(self, mounts=None): 
 
     self.mounts = mounts or {} 
 

 
    def __call__(self, environ, start_response): 
 
     script = environ.get('PATH_INFO', '') 
 
     while '/' in script: 
 
      if script in self.mounts: 
 
       app = self.mounts[script] 
 
       break 
 
      script, last_item = script.rsplit('/', 1) 
 
     else: 
 
      app = self.mounts.get(script, self.not_found) 
 
     return app(environ, start_response) 
 

 
print >> sys.stderr, "app.cgi: server starting", os.environ.get('PATH_INFO') 
 
for k in os.environ: 
 
    print >>sys.stderr, k, os.environ[k] 
 

 
app = Dispatcher({ 
 
    '/xyz/report/v1': report_app(), 
 
    '/xyz/replication/v1': dr_app(), 
 
}) 
 
if __name__ == '__main__': 
 
    os.environ['DEBUG'] = '1' 
 
    from werkzeug.serving import run_simple 
 
    run_simple('0.0.0.0', 5000, app, use_debugger=True, use_reloader=True) 
 
else: 
 
    CGIHandler().run(app)

Pour plus d'informations:

-sh-4.2$ curl -u xyz -H "Origin originhost:3000" --verbose localhost:5000/endpoint/xyz 
 
Enter host password for user 'xyz': 
 
* About to connect() to localhost port 5000 (#0) 
 
* Trying 127.0.0.1... 
 
* Connected to localhost (127.0.0.1) port 5000 (#0) 
 
* Server auth using Basic with user 'xyz' 
 
> GET /endpoint/xyz HTTP/1.1 
 
> Authorization: Basic emt5ajM0aTowNjZCZXg0MjU= 
 
> User-Agent: curl/7.29.0 
 
> Host: localhost:5000 
 
> Accept: */* 
 
> Origin originhost:3000 
 
> 
 
* HTTP 1.0, assume close after body 
 
< HTTP/1.0 401 UNAUTHORIZED 
 
< Content-Type: application/octet-stream 
 
* Authentication problem. Ignoring this. 
 
< WWW-Authenticate: Basic 
 
< Content-Length: 0 
 
< Server: Werkzeug/0.12.2 Python/2.7.5 
 
< Date: Mon, 24 Jul 2017 18:22:26 GMT 
 
< 
 
* Closing connection 0 
 

 

 
-sh-4.2$ curl -u xyz -H "Origin originhost:3000" -H "Access-Control-Request-Method: GET" -H "Access-Control-Request-Headers: X-Requested-With" -X OPTIONS --verbose localhost:5000/endpiont/xyz 
 
Enter host password for user 'xyz': 
 
* About to connect() to localhost port 5000 (#0) 
 
* Trying 127.0.0.1... 
 
* Connected to localhost (127.0.0.1) port 5000 (#0) 
 
* Server auth using Basic with user 'xyz' 
 
> OPTIONS endpoint/xyz HTTP/1.1 
 
> Authorization: Basic emt5ajM0afhtTowNjZCZXg0Mg== 
 
> User-Agent: curl/7.29.0 
 
> Host: localhost:5000 
 
> Accept: */* 
 
> Origin originhost:3000 
 
> Access-Control-Request-Method: GET 
 
> Access-Control-Request-Headers: X-Requested-With 
 
> 
 
* HTTP 1.0, assume close after body 
 
< HTTP/1.0 200 OK 
 
< Content-Type: text/html; charset=utf-8 
 
< Allow: HEAD, GET, POST, OPTIONS, DELETE 
 
< Content-Length: 0 
 
< Server: Werkzeug/0.12.2 Python/2.7.5 
 
< Date: Mon, 24 Jul 2017 18:24:53 GMT 
 
< 
 
* Closing connection 0

Répondre

0

Je pense que votre problème n'est pas à l'extrémité avant, mais sur le côté serveur .. c'est un problème CORS .. vous devez autoriser sur le serveur l'arrière pour obtenir à partir de votre demande avant fin ...

Je ne sais pas comment le faire en Python mais fondamentalement c'est possible dans toutes les langues

+0

Merci beaucoup pour votre réponse rapide. Je suis développeur full stack et bon avec django et flask. Je sais comment faire avec django et flask, mais pas en script cgi. Je demande à mon développeur back-end pour permettre cela à des fins de développement, mais n'obtenez pas de réponses. En attente de quelqu'un côté python export. – Patrick

+0

Très heureux de vous aider –