Skip to content

Commit 31599b0

Browse files
committed
ENH Add support for OPTIONS request
1 parent 6a232e0 commit 31599b0

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Controllers/ApiController.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,26 @@ public function init()
2020
{
2121
parent::init();
2222

23-
// normalize query variables
23+
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
24+
$response = $this->getResponse()
25+
->addHeader('Access-Control-Allow-Origin', '*')
26+
->addHeader("Content-type", "application/json");
27+
28+
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
29+
$response = $response->addHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
30+
}
31+
32+
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
33+
$response = $response->addHeader(
34+
'Access-Control-Allow-Headers',
35+
$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']
36+
);
37+
}
38+
39+
$response->output();
40+
exit;
41+
}
42+
2443
if (strpos($this->request->getHeader('Content-Type'), 'application/json') !== false) {
2544
$input = json_decode(file_get_contents("php://input"), true);
2645

@@ -277,6 +296,7 @@ public function httpError($errorCode = 404, $errorMessage = '')
277296
);
278297

279298
$response->addHeader("Content-type", "application/json");
299+
$response->addHeader('Access-Control-Allow-Origin', '*');
280300

281301
$err = new HTTPResponse_Exception();
282302
$err->setResponse($response);

0 commit comments

Comments
 (0)