Merge pull request #62 from ozh/ozh-jsonp

JSONP support
pull/70/head
Mike Cao 11 years ago
commit 5414b906f6

@ -94,7 +94,7 @@ class Engine {
// Register framework methods
$methods = array(
'start','stop','route','halt','error','notFound',
'render','redirect','etag','lastModified','json'
'render','redirect','etag','lastModified','json','jsonp'
);
foreach ($methods as $name) {
$this->dispatcher->set($name, array($this, '_'.$name));
@ -105,6 +105,7 @@ class Engine {
$this->set('flight.handle_errors', true);
$this->set('flight.log_errors', false);
$this->set('flight.views.path', './views');
$this->set('flight.jsonp.callback', 'jsonp');
$initialized = true;
}
@ -439,6 +440,21 @@ class Engine {
->send();
}
/**
* Sends a JSONP response.
*
* @param mixed $data Data to JSON encode
*/
public function _jsonp($data) {
// Get the callback value (eg '?jsonp=my_function') and pad the output
$callback = $this->request()->query[ $this->get('flight.jsonp.callback') ];
$this->response()
->status(200)
->header('Content-Type', 'application/javascript')
->write($callback.'('.json_encode($data).');')
->send();
}
/**
* Handles ETag HTTP caching.
*

Loading…
Cancel
Save