Skip to content
Advertisement

How to allow CORS in Yii2?

I have a JavaScript frontend that ask for a plain text to my PHP backend.

It almost works excepts because I have a CORS problem. How can allow CORS in my PHP code?

My frontend (JavaScript and jQuery):

JavaScript

My backend (PHP and Yii2):

JavaScript

Advertisement

Answer

Yii2 supports CORS handling, as written on the official documentation.

JavaScript

Alternatively, you could try adding the following headers on your PHP backend:

JavaScript

If doesn’t work, take a look at https://stackoverflow.com/a/9866124/11591212

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement