【发布时间】:2014-03-06 16:42:57
【问题描述】:
我正在做一个项目,如果我使用 REST 与数据库通信,它会生成 XML 代码,下面是它的示例。
<ns2:MultipleResponse xmlns:ns2="http://v1_0.model.service.mydomain.com">
<ns2:AttributeType>
<ID>1</ID>
<Version>0</Version>
<ns2:Name>Type of Address</ns2:Name>
<ns2:Definition>Definition for Type of Address</ns2:Definition>
<ns2:DataType>ShortText</ns2:DataType>
<ns2:MultipleSelect>false</ns2:MultipleSelect>
<ns2:AttributeGroupType>
<ID>1</ID>
<Version>0</Version>
<ns2:Name>Address</ns2:Name>
<ns2:Code>ADR</ns2:Code>
<ns2:Definition>Definition of Address</ns2:Definition>
</ns2:AttributeGroupType>
</ns2:AttributeType>
</ns2:MultipleResponse>
我从 Spring MVC 中的 Web GUI 调用我的 REST。
我使用 jQuery 从另一个选择下拉列表的选择中填充一个选择下拉列表。这适用于 Chrome,但不适用于 FF 或 IE。 我在 FF 中使用 Firebug,它给了我这个错误:
没有找到带有选择器的元素:“AttributeType”
我的 jquery:
<script type="text/javascript">
$(document).ready(function() {
var html = '<option value>Välj</option>';
$('#serviceTypeAttributeGroup').change(function() {
$.ajax({
url: "http://server/project-web/services/rest/auth/v1_0/attributetypes/servicetypeattributegroup/" + $('#serviceTypeAttributeGroup').val(),
type: "GET",
contentType: 'application/xml; charset=utf-8',
dataType: "xml",
success: function(data) {
$(data).find("AttributeType").each(function() {
html = '';
var $attribute = $(this);
var id = $attribute.children("ID:first").text();
var name = $attribute.find("Name:first").text();
html += '<option value="' + id + '">' + name + '</option>';
$('#attributeType').html(html);
});
}
});
return false;
});
$('#attributeType').html(html);
});
我已尝试将“AttributeType”更改为“ns2:AttributeType”、“ns2\\:AttributeType”和“ns2\:AttributeType”,但这不会更改 FF 中的错误消息,并且代码在 Chrome 中停止工作。
当我查看 FF 中的 XML 时,它只显示纯文本,是否有帮助?在 Chrome 中,我看到了所有标签。
我的选择下拉菜单:
<tr>
<th><label for="serviceTypeAttributeGroup"><s:message code="servicetypeattributegroup" />:</label></th>
<td><sf:select path="serviceTypeAttributeGroup.ID" id="serviceTypeAttributeGroup">
<sf:option value="0"> </sf:option>
<sf:options items="${listOfAttributeGroups}" itemLabel="attributeGroupType.name" itemValue="ID" />
</sf:select></td>
</tr>
<tr>
<th><label for="attributeType"><s:message code="attributetype" />:</label></th>
<td><sf:select path="attributeType.ID" id="attributeType">
<sf:option value="0"> </sf:option>
</sf:select></td>
</tr>
有没有人知道哪里出了问题?以及我如何纠正它?
【问题讨论】:
-
您声明在 Firefox 中看不到任何标签。如果是这样,那么我开始怀疑数据根本没有作为 XML 传输,因此问题甚至可能源自服务器端而不是 javascript。我会在服务器端进行一些日志记录,以查看从 chrome 发送的请求和从 Firefox 发送的请求是什么样子的;也许那里有一个重要的区别可能是一个线索。
-
没错,但今天我看到了树形结构,只是一个警告“此 XML 文件似乎没有任何与之关联的样式信息。”但我会检查以防万一。
标签: java jquery rest spring-mvc