2

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
  • not working when setting datatype to xml is weird, because I got it working that way and RafH got it working this way too Commented Feb 22, 2010 at 22:53

3 Answers 3

3

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');
Sign up to request clarification or add additional context in comments.

Comments

2

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.

4 Comments

consider accepting an answer, or at least show us a little gratidute and vote up :)
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 :)
If none of our answers are correct, and you worked it out, then why not mark your answer? :)
Omer, Juraj, I had this same problem and it's not easy to identify, took a few searches to get here. Your combined work got me through, thanks to you both.
1

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');

Comments

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.