diff --git a/laravel/documentation/views/home.md b/laravel/documentation/views/home.md index 27383562..3daf57a9 100644 --- a/laravel/documentation/views/home.md +++ b/laravel/documentation/views/home.md @@ -62,6 +62,10 @@ Sometimes you will need a little more control over the response sent to the brow return Response::json(array('name' => 'Batman')); +#### Returning a JSONP response: + + return Response::jsonp('myCallback', array('name' => 'Batman')); + #### Returning Eloquent models as JSON: return Response::eloquent(User::find(1)); diff --git a/laravel/response.php b/laravel/response.php index ccd53e1f..7979ef46 100644 --- a/laravel/response.php +++ b/laravel/response.php @@ -98,6 +98,26 @@ class Response { return new static(json_encode($data), $status, $headers); } + /** + * Create a new JSONP response. + * + * + * // Create a response instance with JSONP + * return Response::jsonp('myFunctionCall', $data, 200, array('header' => 'value')); + * + * + * @param mixed $data + * @param int $status + * @param array $headers + * @return Response + */ + public static function jsonp($callback, $data, $status = 200, $headers = array()) + { + $headers['Content-Type'] = 'application/javascript; charset=utf-8'; + + return new static($callback.'('.json_encode($data).');', $status, $headers); + } + /** * Create a new response of JSON'd Eloquent models. * @@ -344,4 +364,4 @@ class Response { return $this->render(); } -} \ No newline at end of file +}