A story about how to deal with the CORS problem

It's been a long time since I started to link the front end and server side programs like Rest. I came here and did a lot of pokamis, so as a memorandum.

Have the front create a dummy API that only returns a fixed value json and proceed with the implementation The backend is implemented for prod so that the dummy is dynamic, and finally replaced with the dummy I am producing in a flow like this.

So, the backend uses php, but after a lot of mess

test.php


I'll do various things ~
header('Content-Type: application/json');
echo $json;
exit;

It is the theory to output json as. The front runs test.php as an endpoint with js etc. CORS is almost always a problem.

On the Apache side, thinking that security risks should be considered one after another

Header set Access-Control-Allow-Origin *

Access OK !! from all domains is added to the response header. So, it was bad to forget it completely and write it in php.

header('Content-Type: application/json');
header('Access-Control-Allow-Origin *');
echo $json;
exit;
Access to XMLHttpRequest at 'https://xxxxx.jp/api/test' from origin 
'null' has been blocked by CORS policy: 
The 'Access-Control-Allow-Origin' header contains multiple values '*, *', 
but only one is allowed.

Looking only at the beginning of the error message, I thought that Origin should have been set. If you look closely, it was an error because multiple Access-Control-Allow-Origin headers were set.

Either erase it and solve it, but in this case php is better. I know, but I'm most scared of mistakes in patterns that I don't notice.

**end. ** **

Recommended Posts

A story about how to deal with the CORS problem
How to create a submenu with the [Blender] plugin
A story about how theano was moved with TSUBAME 2.0
[AWS] Wordpress How to deal with "The response is not a correct JSON response"
[systemd] How to deal with the problem that fancontrol does not work after suspending
A story about how to specify a relative path in python.
How to deal with imbalanced data
A story about how Windows 10 users created an environment to use OpenCV3 with Python 3.5
How to deal with imbalanced data
A memo on how to overcome the difficult problem of capturing FX with AI
Finding a solution to the N-Queen problem with a genetic algorithm (1)
A story about porting the code of "Try and understand how Linux works" to Rust
[VLC] How to deal with the problem that it is not in the foreground during playback
[Introduction to Python] How to split a character string with the split function
How to make a command to read the configuration file with pyramid
I wanted to solve the ABC164 A ~ D problem with Python
How to send a request to the DMM (FANZA) API with python
How to add a package with PyCharm
How to solve the bin packing problem
How to deal with enum compatibility errors
[Python] How to deal with module errors
How to read problem data with paiza
How to deal with the problem that the current directory moves when Python is executed from Atom
The 16th offline real-time how to write problem was solved with Python
[Small story] How to save matplotlib graphs in a batch with Jupyter
[Python] Explains how to use the range function with a concrete example
The 16th offline real-time how to write reference problem to solve with Python
Try to solve the traveling salesman problem with a genetic algorithm (Theory)
How to fix the initial population with a genetic algorithm using DEAP
[Introduction to Python] How to write a character string with the format function
The 19th offline real-time how to write reference problem to solve with Python
The 15th offline real-time how to write problem was solved with python
How to deal with memory leaks in matplotlib.pyplot
How to calculate the volatility of a brand
Try to solve the fizzbuzz problem with Keras
How to read a CSV file with Python 2/3
How to share a virtual environment [About requirements.txt]
How to send a message to LINE with curl
How to deal with errors when hitting pip ②
How to draw a 2-axis graph with pyplot
How to make a dictionary with a hierarchical structure.
can't pickle annoy. How to deal with Annoy objects
How to try the friends-of-friends algorithm with pyfof
How to deal with module'tensorflow' has no attribute'〇〇'
How to deal with SessionNotCreatedException when using Selenium
How to deal with the problem that build fails when CI / CD of Python Function with AWS Amplify
A story about implementing a login screen with django
Save the object to a file with pickle
How to create a multi-platform app with kivy
How to Learn Kaldi with the JUST Corpus
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
How to publish a blog on Amazon S3 with the static Blog engine'Pelican'for Pythonista
Try to solve the traveling salesman problem with a genetic algorithm (execution result)
How to deal with the inability to open the lock file / var / lib / dpkg / lock-frontend
[Note] A story about not being able to break through a proxy with pip
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
[Python] How to deal with the is instance error "is instance () arg 2 must be a type or tuple of types"
How to deal with "^ [[A ^ [[B ^ [[C ^ [[D"] when you press the arrow keys when executing python on mac
Read the Python-Markdown source: How to create a parser
How to convert / restore a string with [] in python