views:

272

answers:

3

Hello, this is probably so trivial that I'm ashamed to be asking this...

Why in the world is this not working?

<?xml version="1.0" encoding="UTF-8"?>
<filter id="4" max_values="10">
    <value id="9">strategy</value>
    <value id="11">simulation</value>
    <value id="12">shooter</value>
</filter>

This is the xml response I get when I request the page with:

$.post('afilters/getvaluesXml', {filter_id: fid}, 
            function(response){
                var fields;
                alert(response);

                var filter_id = $(response).find('filter').attr('id');
                var max_values = parseInt($(response).find('filter').attr('max_values'));

                alert('filter_id: '+filter_id+' max_values:'+max_values);

                $(response).find('value').each(function(){
                    var id = $(this).attr('id');
                    var value = $(this).text();

                    if(max_values == 1){
                        fields = fields+'<input type="radio" name="'+filter_id+'" value="'+id+'"/>'+value+'<br/>';
                    } else {
                        fields = fields+'<input type="checkbox" name="'+filter_id+'[]" value="'+id+'"/>'+value+'<br/>';
                    }

                });
                //alert(fields);
                $('#'+filter_id+'_values').text(fields);
            });

Everything works fine except I am unable to get the filter_id and max_values attributes. This is the alert box content:

filter_id: null max_values:NaN

And, why when I specify the datatype "xml" as described in the jquery .post() docu, nothing gets back to me (no response is ever received - callback is never executed).

+1  A: 

Maybe not the best practice but it works

var filter_id = $(response).find("value:first").parent().attr('id');
var max_values = $(response).find("value:first").parent().attr('max_values');
RafH
+2  A: 

I have tried setting dataType to xml and it worked like a charm, have a look at this snippet:

$.post('afilters/getvaluesXml', {filter_id: fid}, function(response){
  var filter_id = $('filter', response).attr('id');
  var max_values = parseInt($('filter', response).attr('max_values'));
  alert('filter_id: '+filter_id+' max_values:'+max_values);
 }, 'xml');
Juraj Blahunka
works for me too with xml datatype
RafH
+1  A: 

Damn... Well you are all right. It is just me who forgot to set the proper header before outputting the xml...

So setting the output header to "content-type: text/xml" entirely fixed this problem.

Seems jQuery won't execute the callback when the content type is not as expected.

It is however weird that everything apart those two root node attributes worked :)

Thank you for pointing me in the right direction.

Omer Sabic
consider accepting an answer, or at least show us a little gratidute and vote up :)
Juraj Blahunka
I voted up your posts. However the headers were the problem so for the sake of someone trying to identify the solution, I should mark my own answer as the "correct answer" though I don't want any attribution for it :)
Omer Sabic
If none of our answers are correct, and you worked it out, then why not mark your answer? :)
Juraj Blahunka