Gå til hovedindhold

JSONP support for API - Indlæg / Feature Request - Deskpro Support

36
Currently the application doesn&#039;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-&gt;container-&gt;get(&#039;response&#039;);<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-&gt;request-&gt;isXmlHttpRequest() || isset($_SERVER[&#039;HTTP_ACCEPT&#039;]) &amp;&amp; (strpos($_SERVER[&#039;HTTP_ACCEPT&#039;], &#039;application/json&#039;) !== false)) {<br /><br /> $response-&gt;headers-&gt;set(&#039;Content-Type&#039;, &#039;application/json&#039;);<br /><br /> } else {<br /><br /> $response-&gt;headers-&gt;set(&#039;Content-Type&#039;, &#039;text/plain&#039;);<br /><br /> }<br /><br /> <br /><br /> $response-&gt;setStatusCode($status_code);<br /><br /> <br /><br /> if (is_array($content)) {<br /><br /> $content = json_encode($content);<br /><br /> }<br /><br /> <br /><br /> $response-&gt;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-&gt;container-&gt;get(&#039;response&#039;);<br /><br /> <br /><br /> if (isset($_GET[&quot;callback&quot;])) {<br /><br /> return $this-&gt;createJsonpResponse($content, $status_code, $_GET[&#039;callback&#039;]);<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-&gt;request-&gt;isXmlHttpRequest() || isset($_SERVER[&#039;HTTP_ACCEPT&#039;]) &amp;&amp; (strpos($_SERVER[&#039;HTTP_ACCEPT&#039;], &#039;application/json&#039;) !== false)) {<br /><br /> $response-&gt;headers-&gt;set(&#039;Content-Type&#039;, &#039;application/json&#039;);<br /><br /> } else {<br /><br /> $response-&gt;headers-&gt;set(&#039;Content-Type&#039;, &#039;text/plain&#039;);<br /><br /> }<br /><br /> <br /><br /> $response-&gt;setStatusCode($status_code);<br /><br /> <br /><br /> if (is_array($content)) {<br /><br /> $content = json_encode($content);<br /><br /> }<br /><br /> <br /><br /> $response-&gt;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.

Tilføj en kommentar

Log ind, eller opret dig for at skrive en kommentar.

Har du brug for en påmindelse om adgangskode?