0

I'm getting confused by AJAX/S2 when I need to pass some arguments retrieved with JavaScript to a method in the controller.

In my script I have at some point, I get the position var:

function handle_geolocation_query(position){ 

    alert('Lat: ' + position.coords.latitude + ' ' +  
          'Lon: ' + position.coords.longitude);


    $.when( getLakes(position.coords.latitude, position.coords.longitude)).done(function(result1) {
        console.log(result1);
    });

};

function getLakes(lat, long) {
    return $.ajax({
            url: "{{ path('getLakes') }}",
            data: { lat:lat, lng: lng },
            dataType: 'jsonp'
        });
};

then the router is set that way:

getLakes:
    pattern:  /lakes
    defaults: { _controller: PondipGeolocBundle:Default:getLakesSurrounding }

And In my controller I'm supposed to return an array:

public function getLakesSurrounding($lat=0, $lng=0, $limit = 50, $distance = 50, $unit = 'km')
    {

            $lat = $this->getRequest()->get('lat'); 
    $lng = $this->getRequest()->get('lont');

        $return= array( '1'=>   array(  'title' => 'lake1',
                                        'venue' => 'lake2',
                                        'dist' => 'lake3',
                                        'species' => 'lake4',
                                        'stock' => 'lake4'),

                        '2'=>   array(  'title' => 'lake1',
                                        'venue' => 'lake2',
                                        'dist' => 'lake3',
                                        'species' => 'lake4',
                                        'stock' => 'lake4'),

                        '3'=>   array(  'title' => 'lake1',
                                        'venue' => 'lake2',
                                        'dist' => 'lake3',
                                        'species' => 'lake4',
                                        'stock' => 'lake4'),

                        '4'=>   array(  'title' => 'lake1',
                                        'venue' => 'lake2',
                                        'dist' => 'lake3',
                                        'species' => 'lake4', 
                                        'stock' => 'lake4'),

                        '5'=>   array(  'title' => 'lake1',
                                        'venue' => 'lake2',
                                        'dist' => 'lake3',
                                        'species' => 'lake4',
                                        'stock' => 'lake4')
                        );


        $return=json_encode($return); //jscon encode the array
        return new Response($return,200,array('Content-Type'=>'application/json')); //make sure it has the correct content type

    }

And then I'd like to pass it to another function that would set up the template with moustache to print it in the view (I'm not here yet ..)

My question is: I can't pass the needed lat and long data to my function. the router gets mad, the controller doesnt get anything and I don't get anything back from that script. EDIT:I found my way to pass the variables, but I still can't get any response from my controller, nohing happen and the $.when does not execute any callback

I'm fairly new with AJAX, please advise.

2
  • If you're satisfied with the answer, please accept it, so others know, it no longer needs answering. Accepted answers are displayed first and others know if the answer is relevant. Commented Mar 18, 2013 at 17:42
  • I was still working on it, Thanks for your help! Commented Mar 18, 2013 at 18:41

1 Answer 1

1

Have a look at FOSJsRoutingBundle. With it you can use router from JS:

Routing.generate('my_route_to_expose', { "id": 10, "foo": "bar" });
Sign up to request clarification or add additional context in comments.

2 Comments

This worked for me without the need to install this bundle $(window.location).attr('href', '{{ path('planner_delete', {'id': entity.id}) }}');
@elvismdev Your solution only works for situations when route params are known at the twig rendering, not if these params are obtained via AJAX request. Read the question first before posting, please.

Your Answer

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.