获取根节点属性,jQuery/XML
这可能是如此微不足道,以至于我羞于问这个……
为什么这不起作用?
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我尝试将 dataType 设置为 xml,它的工作就像一个魅力,看看这个片段:
I have tried setting dataType to xml and it worked like a charm, have a look at this snippet:
该死……好吧,你没事吧。只是我忘记在输出 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.
也许不是最佳实践,但它确实有效
Maybe not the best practice but it works