0

I'm currently working on a dropdown and having a lot of trouble. Its a dependent dropdown and so far I can get the correct data when I make a selection on the first dropdown but I cannot display it on the second dropdown. I know I get the data because I see it while debugging on chrome. This is an Example of what i get:

Object {readyState: 4, responseText: "{"Hardware":1,"Software":2,"Other":3,"option":10}Page generated in 0.2831 seconds.<br><br><br>", status: 200, statusText: "OK"}
parsererror
Object {readyState: 4, responseText: "{"General":5,"Books":6}Page generated in 0.2902 seconds.<br><br><br>", status: 200, statusText: "OK"}
parsererror

This is my javascript code:

<script type="text/javascript">
$(document).ready(function () {
    $("#request_department").change(function(){
        var data = {
            department_id: $(this).val()
        };
        $.ajax({
            type: 'POST',
            {#url: '{{ path("select_options") }} ?category_id' + dep,#}
            url: "{{ url('select_options') }}?dep_id=" + data.department_id,
            data: data,

            success: function(data) {
                window.alert('hi');
                var $option_selector = $('#request_option');

                $option_selector.html('<option>Option</option>');

                for (var i=0, total = data.length; i < total; i++) {
                    $option_selector.append('<option value="' + data[i].id + '">' + data[i].option + '</option>');
                }
            },
            error: function(xhr, error){
                console.debug(xhr); console.debug(error);
            },
        });
    });
});
</script>

And this the code in the controller:

public function optionAjaxAction(Request $request)
    {

        if (!$request->isXmlHttpRequest()) {
            throw new NotFoundHttpException();
        }

        $id = $request->query->get('dep_id');

        $result = array();

        // Return a list of options, based on the selected department
        $repo = $this->getDoctrine()->getManager()->getRepository('MyBundle:RequestOption');
        $option = $repo->findByDepartment($id, array('department' => 'asc'));
        //var_dump($hardware);
        foreach ($option as $o) {
            $result[$o->getOption()] = $o->getId();
        }

        return new JsonResponse($result);
    }

Im desperate. I would really appreciate any help! Thanks in advance (:

1
  • 1
    You need to find out where Page generated in #.#### seconds. is being injected into the final response. Maybe you (or a loaded bundle) have set up a response filter. Commented Jun 13, 2016 at 12:30

1 Answer 1

2

As you are producing json and at your client you don't have dataType set, so, dataType='text' is considered as a intelligent guess.

It would be better if you need to add dataType:'json', in your ajax call.


As a side not you don't have to send same data twice. You are sending it in the ajax url and in the data object too.

Try removing from url:

var data = {
    dep_id: $(this).val()
};


 url: "{{ url('select_options') }}",
 data: data,
Sign up to request clarification or add additional context in comments.

10 Comments

Already did, did not work. Actually I erased it for this question because I found in another question that that was the problem. :/ Any more suggestions?
There might be problems with the javascript, but it's certainly not the most immediate problem here. The server response contains extra text, making the json invalid. Before anything, this would need to be fixed.
@OmarAguinaga Your problem is not in the javascript! Your server response is invalid JSON. It contains extra text "Page generated in 0.2902 seconds.<br><br><br>". You should not try to fix that on the client-side.
@OmarAguinaga If you can't change the fact that something (even if it's manually added in app_dev.php) is adding extra content to every response sent by your application, then you'll never be able to return valid json. Are you sure this is a necessity?
Thanks to everyone! Now it works, everyone help in a way. A lot of good answer that lead to the solution. Thanks!
|

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.