获取根节点属性,jQuery/XML

发布于 2024-08-23 06:48:12 字数 1649 浏览 8 评论 0原文

这可能是如此微不足道,以至于我羞于问这个……

为什么这不起作用?

<?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>

这是我请求页面时得到的 xml 响应:

$.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);
            });

一切正常,除了我无法获取 filter_id 和 max_values 属性。 这是警报框内容:

filter_id: null max_values:NaN

而且,为什么当我指定数据类型“xml”时,如 jquery .post() docu,没有任何回复给我(没有收到任何响应 - 回调永远不会执行)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏佲洛 2024-08-30 06:48:12

我尝试将 dataType 设置为 xml,它的工作就像一个魅力,看看这个片段:

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

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');
蓝眼泪 2024-08-30 06:48:12

该死……好吧,你没事吧。只是我忘记在输出 xml 之前设置正确的标头...

因此将输出标头设置为“content-type: text/xml”完全解决了这个问题。

当内容类型不符合预期时,jQuery 似乎不会执行回调。

然而,奇怪的是,除了这两个根节点属性之外的所有内容都有效:)

谢谢您为我指明了正确的方向。

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.

往事随风而去 2024-08-30 06:48:12

也许不是最佳实践,但它确实有效

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

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文