Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORS, nefungující Access-Control-Allow-Origin #20

Open
matak opened this issue May 4, 2018 · 1 comment
Open

CORS, nefungující Access-Control-Allow-Origin #20

matak opened this issue May 4, 2018 · 1 comment

Comments

@matak
Copy link

matak commented May 4, 2018

nenapadá někoho proč mi nefunguje CORS? potřebuji se k api dostat z jquery

`
public function actionCreate()
{
$response = $this->getHttpResponse();
$response->addHeader('Access-Control-Allow-Origin', "");
$response->addHeader('Access-Control-Allow-Methods', "
");
$response->addHeader('Access-Control-Allow-Headers', 'X-Requested-With');

	$post = $this->getRequest()->getPost();
	$this->sendJson($post);
}

`

zdá se že nette nevrací správné hlavičky

tady je jquery požadavek

`
(function ($) {
$.ajaxPost = function (url, data, success) {
var ajaxSettings = {
url: url,
method: "POST",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
success: function (result)
{
if (result.status !== "ok") {
alert("Napodařilo se provést požadované úpravy!");
}

						if (result.info) {
							alert(result.info);
						}

						if (success) {
							success(result);
						}
					},
					error: function (xhr, status) {
						alert("Nebylo možné zpracovat data!");
					}
				};

				$.ajax(ajaxSettings);
			};
		})(jQuery);		

`

díky za tipy jak tohle řešíte

@rosakovnik
Copy link

rosakovnik commented May 5, 2019

header('Access-Control-Allow-Headers: accept, content-type, authorization'); header('Access-Control-Allow-Methods: GET,POST,OPTIONS,DELETE,PUT'); header('Access-Control-Allow-Origin: *'); if ("OPTIONS" === $_SERVER['REQUEST_METHOD']) { return; }
Používám v index.php hned na začátku tohle, ty allow headers si uprav podle toho, co potřebuješ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants