JSONP support for API - Opinie / Feature Request - Deskpro Support
JSONP support for API Collecting Feedback
Currently the application doesn't return properly structured JSONP responses. I have another application running on a different subdomain and need the ability to do cross domain requests. Looking through the source code it looks like there is no where in the code that is set to return the json padded with a function. The function is there but in the handling of requests it is hard coded to always return json without the padded callback function.<br /><br />
<br /><br />
My quick fix for this is to edit the HttpKernel/Controller/Controller.php file. I changed the function createJsonResponse function. The current code is:<br /><br />
<br /><br />
public function createJsonResponse($content, $status_code = 200)<br /><br />
{<br /><br />
$response = $this->container->get('response');<br /><br />
<br /><br />
// Because IE will sometimes prompt to download json when using iframe transport for ajax if we dont do this<br /><br />
if ($this->request->isXmlHttpRequest() || isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {<br /><br />
$response->headers->set('Content-Type', 'application/json');<br /><br />
} else {<br /><br />
$response->headers->set('Content-Type', 'text/plain');<br /><br />
}<br /><br />
<br /><br />
$response->setStatusCode($status_code);<br /><br />
<br /><br />
if (is_array($content)) {<br /><br />
$content = json_encode($content);<br /><br />
}<br /><br />
<br /><br />
$response->setContent($content);<br /><br />
<br /><br />
return $response;<br /><br />
}<br /><br />
<br /><br />
I changed the function to simply return the already programmed createJsonpResponse function if there is a callback function specified.<br /><br />
<br /><br />
public function createJsonResponse($content, $status_code = 200)<br /><br />
{<br /><br />
$response = $this->container->get('response');<br /><br />
<br /><br />
if (isset($_GET["callback"])) {<br /><br />
return $this->createJsonpResponse($content, $status_code, $_GET['callback']);<br /><br />
}<br /><br />
<br /><br />
// Because IE will sometimes prompt to download json when using iframe transport for ajax if we dont do this<br /><br />
if ($this->request->isXmlHttpRequest() || isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {<br /><br />
$response->headers->set('Content-Type', 'application/json');<br /><br />
} else {<br /><br />
$response->headers->set('Content-Type', 'text/plain');<br /><br />
}<br /><br />
<br /><br />
$response->setStatusCode($status_code);<br /><br />
<br /><br />
if (is_array($content)) {<br /><br />
$content = json_encode($content);<br /><br />
}<br /><br />
<br /><br />
$response->setContent($content);<br /><br />
<br /><br />
return $response;<br /><br />
}<br /><br />
<br /><br />
The reason I am requesting this update is because everytime I update the system I have to re-add the change.<br /><br />
<br /><br />
Thanks.
Dodaj komentarz
Zaloguj lub zarejestruj się, by złożyć komentarz.