3

I'm trying to return JSONP from Symfony2. I can return a regular JSON response fine, but it seems as if the JSON response class is ignoring my callback.

$.ajax({
 type: 'GET',
 url: url,
 async: true,
 jsonpCallback: 'callback',
 contentType: "application/json",
 dataType: 'jsonp', 
 success: function(data) 
 { 
 console.log(data);
 },
 error: function() 
 {
 console.log('failed');
 }
 }); 

Then in my controller:

$callback = $request->get('callback'); 
$response = new JsonResponse($result, 200, array(), $callback);
return $response;

The response I get from this is always regular JSON. No callback wrapping.

The Json Response class is here:

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/JsonResponse.php

Cœur
38.9k25 gold badges206 silver badges281 bronze badges
asked Oct 1, 2012 at 9:40

2 Answers 2

13

As the docs says:

$response = new JsonResponse($result, 200, array(), $callback);

You're setting the callback method as the $headers parameter.

So you need to:

$response = new JsonResponse($result, 200, array());
$response->setCallback($callback);
return $response;
answered Oct 1, 2012 at 9:55
0
1

The JsonResponse's constructor doesn't take the callback argument. You need to set it via a method call:

$response = new JsonResponse($result);
$response->setCallback($callback);
return $response;
answered Oct 1, 2012 at 9:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.