Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

XMLHttpRequest ... Origin http://localhost is not allowed by Access-Control-Allow-Origin.

1 réponse
Avatar
Une Bévue
j(ai un script js qui fait un xhr sur un serveur local (python/mango)
"http://localhost:27080/_hello"

si je pointe mon butineur directement sur cette URL, j'obtiens :
{"ok" : 1, "msg" : "Uh, we had a slight weapons malfunction, but uh...
everything's perfectly all right now. We're fine. We're all fine here
now, thank you. How are you?"}

maintenant si je le fais par js, en xhr, j'ai l'erreur suivante :
XMLHttpRequest cannot load http://localhost:27080/_hello. Origin
http://localhost is not allowed by Access-Control-Allow-Origin.

j'ai googelisé, sans trouvé de solution (en js), je sais qu'il faut
avoir un header avec :
Access-Control-Allow-Origin: *

ben je ne pige pas vraiment et surtout comment et où mettre ce header ?

mon serveur "sleepy.mongoose-master" est lancé par :
.-[yt@d620.local:~/Sites/sleepy.mongoose-master]-[14:13:04]


'->$ python httpd.py

il me répond :

=================================
| MongoDB REST Server |
=================================

listening for connections on http://localhost:27080

1.0.0.127.in-addr.arpa - - [07/Sep/2013 14:50:47] "GET /_hello HTTP/1.1"
200 -
1.0.0.127.in-addr.arpa - - [07/Sep/2013 14:51:10] "GET /_hello HTTP/1.1"
200 -
1.0.0.127.in-addr.arpa - - [07/Sep/2013 14:51:11] code 404, message File
Not Found: favicon.ico


le dernier message "File Not Found" est curieux car je ne lui demande
pas favicon.ico mais à Apache2...

1 réponse

Avatar
Une Bévue
.-[:~/Sites/sleepy.mongoose-master]-[14:13:04]
'->$ python httpd.py




en lisant le contenu de
~/Sites/sleepy.mongoose-master/sleepymongoose/httpd.py

je remarque les lignes :

if o == "-x" or o == "--xorigin":

MongoHTTPRequest.response_headers.append(("Access-Control-Allow-Origin","*"))


donc, je lance le serveur par :
python httpd.py --xorigin

et ça roule, as expected...