The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

How can i make a callback event(JSONP) with slim framework

thanos's Avatar

thanos

07 Feb, 2012 09:39 PM

Hello,
Im trying to make a JSONP call but i cant understand how to pass the 'callback' object.
Is possible to do it with slim framework?

$.getJSON( '/kronos', function(data) {

// do something with data

},"json");

$app->get('/kronos',function() {

 $arr = array( 'item_id' => 5, 'price' => 88,'quantity' =>'Best');
 echo 'callback'. ' (' . $arr  . ');';

});

  1. Support Staff 1 Posted by Josh Lockhart on 07 Feb, 2012 09:52 PM

    Josh Lockhart's Avatar

    Usually the callback is passed as a query parameter. Assuming the query parameter is called "callback", you can do this:

    $app->get('/foo', function () use ($app) {
        $callback = $app->request()->get('callback');
        $data = array('foo' => 'bar');
        $app->contentType('application/javascript');
        echo sprintf("%s(%s)", $callback, json_encode($data));
    });
    
  2. 2 Posted by thanos on 07 Feb, 2012 10:28 PM

    thanos's Avatar

    what about the query function. i have to make a refernce of callback in there like this?

    $.getJSON(url+'&callback=?', function(data){

    });

  3. Support Staff 3 Posted by Josh Lockhart on 07 Feb, 2012 10:45 PM

    Josh Lockhart's Avatar

    In JS, if you manually construct the URL, you will need to append "?callback=foo". This will return "foo({"foo": "bar"})" in the HTTP response body so it can be eval'd client-side. Usually, I prefer to use jQuery's $.getJSON or $.ajax helpers (making sure the dataType is "jsonp"); in which case, jQuery will append the callback query parameter automatically.

  4. 4 Posted by thanos on 07 Feb, 2012 10:55 PM

    thanos's Avatar

    Thank's for your help Josh. i will work it around tomorrow.

  5. 5 Posted by thanos on 08 Feb, 2012 01:36 PM

    thanos's Avatar

    So this is solved for me.
    I tested on browsers and it retrieves the data perfectly.
    My final code is:

    $.getJSON( URL + '?callback=kronos', function(data) {
       // do something with data
    });
    
    $app->get('/kronos',function() use ($app)  {
        $callback = $app->request()->get('callback');
        $data = array('foo' => 'bar');
        $app->contentType('application/javascript');
        echo sprintf("%s(%s)", $callback, json_encode($data) );
    });
    
  6. 6 Posted by hexa2k9 on 12 Feb, 2012 12:19 PM

    hexa2k9's Avatar

    Hi,

    just in case you don't alway need to return jsonp for any of your routes (which is the case for my application) you can put that jsonp wrapping stuff into a separate function.

    This should look about like that:

    function jsonpWrap($jsonp) {
      $app = Slim::getInstance();
    
      if (($jsonCallback = $app->request()->get('jsoncallback')) !== null) {
        $jsonp = sprintf("%s(%s);", $jsonCallback, $jsonp);
        $app->response()->header('Content-type', 'application/javascript');
      }
      return $jsonp;
    }
    

    Within your Routes you simply call that function:

    $app->response()->body(jsonpWrap(json_encode($payload)));
    

    This works for me quite great and it's still possible to just return json.

    Best,
    Chris

  7. Andrew Smith closed this discussion on 02 Aug, 2012 11:04 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac