Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am really new with Symfony framework I am just trying to implement a simple ajax example:

Routing:

DartDartBundle_data_user:
    pattern:  /data/user
    defaults: { _controller: DartDartBundle:Data:user }
    requirements:
        _method:  POST

DataController:

namespace Dart\DartBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;

class DataController extends Controller
{
    public function userAction(){      
        $content = array('success' => true, 'name' => 'luis');
        $response = new JsonResponse($content,200);
        $response->headers->set('Content-Type', 'application/json; Charset=UTF-8');
    return $response;
    }
}

And the ajax call:

$.ajax({
    type : 'GET',
    dataType: 'json',
    url : 'http://symfony.dartintelligence.com/app_dev.php/data/user',
    success : function(data){
        console.log("OK");
        console.log(data);  
    },
    error: function (xhr, ajaxOptions, thrownError){   
        console.log("SOME ERROR OCURRED");
        console.log(thrownError);
    }
});

But it always fires the error function. I don't know why. I've try without specifying jston dataType and using Response instead of JsonResponse.

I don't know whats wrong.

share|improve this question
 
if you enter 'symfony.dartintelligence.com/app_dev.php/data/user'; into your browser, do you get a valid json output? also, what is the error that is returned? –  user2930475 yesterday
 
In the routing you are restricting the Ajax call to 'POST' and in javascript you are issuing a GET. Try changing to $.ajax{type:'POST'... –  Alberto Gaona yesterday
 
Yep, both are right. If I use browser it gives me the expected results (if I set up it as GET method). And sorry I mixed things up, I've tried method restricted GET or POST and trying both Ajax calls using GET or POST with the same result. Error handler fires. –  luis83rs yesterday
 
Can you post the text of the error? –  mattexx yesterday
 
Screen capture of Firebug with the "error" dropbox.com/s/g77xbb9d2nyl78e/screen_capture_20131120.png –  luis83rs 16 hours ago

2 Answers

First

Route is configured as POST - only

The Ajax request is a GET request

try to remove the requirement

Second

Try to access the url directly in your browser to check if you configured the path right and you can access the controller.

share|improve this answer
 
Thanks for the feedback, as I said I mixed up things because I was trying both methods, GET and POST. But no luck. I am still getting the same error using both methods, its returns status 200 but it fires error handler of jQuery Ajax callback. No idea why. –  luis83rs yesterday
 
 
And accessing the url directly works, yep: dropbox.com/s/3lyf5zgw4djoi83/screen_capture_20131120_2.png. Ajax is still having the same problem :( –  luis83rs 15 hours ago
 
Is the Ajax request from the same origin or is this a cross site request ? If you open the request in firebug (arrow sign) what informations can you read out of it (header -> response) –  nixoschu 14 hours ago

The request is made from the same server but with a different domain. The request is not using any vhost or domain, its uses server's ip and the htdocs folder.

Symfony is placed in a folder which has a vhost alias and its accessed using a domain (i.e. symfony.dartintelligence.com/app_dev.php/data/user). The folder in the server is inside /DART root folder. So its ./DART/symfony....

The firebug info for either of the methods. For instance, for GET method: https://www.dropbox.com/s/r9o6kbsnlyhibxo/screen_capture_20131120_GET_method.png

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.