Skip to content

Commit

Permalink
Disambiguate reference to the jsonp padding prefix misleadingly refer…
Browse files Browse the repository at this point in the history
…red to as callback. Supply correct Content-Type response header for json but fall back on Content-Type text/javascript for jsonp as the proposed Content-Type application/json-p has not been adopted as yet. Documentation elaborated to further attempt to avoid confusion while keeping the implementation minimalist and klein.
  • Loading branch information
nickl- committed May 12, 2012
1 parent 4bfbfa7 commit 634774f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -261,7 +261,7 @@ $response->
flash($msg, $type = 'info', $params = array() // Set a flash message
file($path, $filename = null) // Send a file
noCache() // Tell the browser not to cache the response
json($object, $callback = null) // Send an object as JSON(p)
json($object, $jsonp_prefix = null) // Send an object as JSON or JSONP by providing padding prefix
markdown($str, $args, ...) // Return a string formatted with markdown
code($code = null) // Return the HTTP response code, or send a new code
redirect($url, $code = 302) // Redirect to the specified URL
Expand Down
11 changes: 6 additions & 5 deletions klein.php
Expand Up @@ -438,16 +438,17 @@ public function file($path, $filename = null, $mimetype = null) {
readfile($path);
}

//Sends an object as json
public function json($object, $callback = null) {
//Sends an object as json or jsonp by providing the padding prefix
public function json($object, $jsonp_prefix = null) {
$this->discard();
$this->noCache();
set_time_limit(1200);
$json = json_encode($object);
header('Content-Type: application/json');
if (null !== $callback) {
echo $callback($json);
if (null !== $jsonp_prefix) {
header('Content-Type: text/javascript'); // should ideally be application/json-p once adopted
echo "$jsonp_prefix($json);";
} else {
header('Content-Type: application/json');
echo $json;
}
}
Expand Down

0 comments on commit 634774f

Please sign in to comment.