Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #43 from nickl-/master

Fixes to $response->json()
  • Loading branch information...
commit 59b17ab997105c63f013cb505422ae434a768eb6 2 parents 8978056 + 634774f
@chriso authored
Showing with 7 additions and 5 deletions.
  1. +1 −1  README.md
  2. +6 −4 klein.php
View
2  README.md
@@ -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
View
10 klein.php
@@ -438,15 +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);
- 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;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.