0

In CakePHP, the framework detects what data type to return in using either the url with the extension .json OR they search for the Accepts http header.

In JSONP, I cannot modify the hTTP header. https://stackoverflow.com/a/19604865/80353

I want to avoid using .json

I then tried to set the viewClass using the code below:

    if ($this->request->is('ajax')) {
        $this->log('enter here');
        $this->viewClass = 'Json';
    }

Subsequently, I realized that the request will not work because the header does not contain the XMLHTTPRequest.

My questions are:

1) how do I return Json data for jsonp requests without resorting to the extension in the url?

2) is there a way for cakephp to detect a jsonp request? My gut says this is not possible.

Community
  • 1
  • 1
Kim Stacks
  • 10,202
  • 35
  • 151
  • 282

2 Answers2

0

I needed a jsonp to work with the boundingbox feature of openlayers and I made something like this:

In the Controller function:

$this->layout = false;
// get the callback from the request
$callback = $this->request->query['callback'];

$data = // do something usefull...

$this->set('callback', $callback);
$this->set('json', $data);
$this->render('../Elements/jsonp');

And the element:

<?php
$tmp = json_encode($json);

/* Generic JSON template */
if(!isset($debug)){
    header("Pragma: no-cache");
    header("Cache-Control: no-store, no-cache, max-age=0, must-revalidate");
    //header('Content-Type: application/json');
}

echo $callback . '({ "type": "FeatureCollection",
  "features": ' . $tmp . '});';
;
?>

as you can see I did some lazy-ass programming to 'build' the output format here in the element. This is easily modified for other forms of data.

EDIT:

You don't need to use an extension, since your calling a function in a Controller.

Using the option in Config/routes.php http://book.cakephp.org/2.0/en/development/routing.html#file-extensions

Router::parseExtensions('xml', 'json');

The functionname in the controller will be:

json_myfunction(){ ...

And the URL will become:

localhost/json/mycontroller/myfunction?param1=...
Jeroen
  • 1,638
  • 3
  • 23
  • 48
  • Hi Jeroen, what is the url you use? By the way, I think your answer is more for how to use jsonp in cakephp in general. http://stackoverflow.com/q/14657154/80353 I already know how to do this. What I am asking in particular is to do this without resorting to making the url end with .json – Kim Stacks Nov 01 '13 at 15:29
  • @KimSia: Ah, ok,you don't need any extension, since your calling a function. I use the default routes for json, so the Controller function has the name json_[FunctionName] and the URL becomes: localhost/json/[ControllerName]/[FunctionName]?[Params]. I'll update the answer – Jeroen Nov 01 '13 at 15:43
  • Hi Jeroen, usually I have the same action serving as ajax endpoint and normal webpage. So in my case, I really see no point to separate into json_Action and normal action. I will end up using the .json anyway – Kim Stacks Nov 02 '13 at 16:46
0

A JSONP request would contain a querystring var (commonly named callback) specifying the javascript callback function name. So you can check for that:

if ($this->request->query('callback')) { /*do stuff*/ }

ADmad
  • 8,102
  • 16
  • 18