jqGrid卡在加载...因为XML无效

时间:2015-09-01 18:13:32

标签: javascript jquery xml jqgrid

我有一个带有搜索框的表单,当触发输入字段中输入的值发送到Web服务时,Web服务将XML数据返回给我,我将其输入到jqGrid中以显示结果列表。我得到了正确显示的结果,我遇到的问题是找不到与搜索条件匹配的结果,当发生这种情况时,Web服务仍会返回带有常用标题但没有任何数据集的XML。会发生什么是jqGrid显示"正在加载......"框和它无限期地保持这种状态,搜索功能在网页重新加载之前不会起作用。

这是相关的JS代码:

$("#buttonSearch").on("click", function() {
    $("#jqSearch").setGridParam({url:"http://website/webservice=filter="+$("#inputSearch").val(),datatype:"xml" }).trigger("reloadGrid");
    $("#divSearchDialog").dialog("open");
});

$("#jqSearch").jqGrid({
    ajaxGridOptions: {
        contentType: 'application/soap+xml; charset=utf-8',
        timeout: 10000
    },
    datatype: 'local',
    xmlReader: { root:"NewDataSet", row:"Table" , repeatitems:false},
    colNames: ["_ItemGuid", "AssetType", "HostName", "SerialNumber", "Manufacturer", "Model", "Status", "SystemType", "OSName", "OSVersion", "OSType", "Env", "Details"  ],
    colModel: [
        { name: '_ItemGuid', index: '_ItemGuid', hidden:true, xmlmap:"_ItemGuid" },
        { name: 'AssetType', index: 'AssetType', xmlmap:"AssetType", width: 70 },
        { name: 'HostName', index: 'HostName', xmlmap:"HostName", width: 50 },
        { name: 'SerialNumber', index: 'SerialNumber', xmlmap:"SerialNumber", width: 180 },
        { name: 'Manufacturer', index: 'Manufacturer', xmlmap:"Manufacturer", width: 70 },
        { name: 'Model', index: 'Model', xmlmap:"Model", width: 80 },
        { name: 'Status', index: 'Status', xmlmap:"Status", width: 40 },
        { name: 'SystemType', index: 'SystemType', hidden:true, xmlmap:"SystemType" },
        { name: 'OSName', index: 'OSName', hidden:true, xmlmap:"OSName" },
        { name: 'OSVersion', index: 'OSVersion', hidden:true, xmlmap:"OSVersion" },
        { name: 'OSType', index: 'OSType', hidden:true, xmlmap:"OSType" },
        { name: 'Env', index: 'Env', hidden:true, xmlmap:"Env" },
        { name: 'Details', index: 'Details', hidden:true, xmlmap:"Details" },
    ],
    gridView: true,
    loadonce: true,
    viewrecords: true,
    emptyrecords: 'No results found matching your search criteria.',
    width: 960,
    height: 250,
    rowNum: 50,
    loadError: function(xhr,st,err) { 
        if (xhr.status!=200) {
             alert("Error "+xhr.status+" "+xhr.statusText+" loading grid "+this.id);
        }
    }
});

这就是有效的XML:

<?xml version="1.0" encoding="utf-8" ?> 
<NewDataSet>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
            <xs:complexType>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="Table">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="_ItemGuid" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" minOccurs="0" /> 
                                <xs:element name="AssetType" type="xs:string" minOccurs="0" /> 
                                <xs:element name="HostName" type="xs:string" minOccurs="0" /> 
                                <xs:element name="SerialNumber" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Manufacturer" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Model" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Status" type="xs:string" minOccurs="0" /> 
                                <xs:element name="SystemType" type="xs:string" minOccurs="0" /> 
                                <xs:element name="OSName" type="xs:string" minOccurs="0" /> 
                                <xs:element name="OSVersion" type="xs:string" minOccurs="0" /> 
                                <xs:element name="OSType" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Env" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Details" type="xs:string" minOccurs="0" /> 
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    <Table>
        <_ItemGuid>912da2ab-d717-678c-a5ad-bb1ece8bcf5f</_ItemGuid> 
        <AssetType>Virtual Machine</AssetType> 
        <HostName>MYSERVER</HostName> 
        <SerialNumber>VMware-42 21 8c 09 00 01 02 03-04 05 06 07 08 09 10 11</SerialNumber> 
        <Manufacturer>VMware, Inc.</Manufacturer> 
        <Model>VMware Virtual Platform</Model> 
        <Status>Active</Status> 
        <SystemType>Win64</SystemType> 
        <OSName>Windows Server 2008 R2 Standard</OSName> 
        <OSVersion>6.1</OSVersion> 
        <OSType>Win</OSType> 
        <Env>PROD</Env> 
        <Details>PROD</Details> 
    </Table>
</NewDataSet>

如果没有找到结果,XMl看起来像这样,则缺少Table元素,这些是我在xmlreader中浏览的行

<?xml version="1.0" encoding="utf-8" ?> 
<NewDataSet>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
            <xs:complexType>
                <xs:choice minOccurs="0" maxOccurs="unbounded">
                    <xs:element name="Table">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="_ItemGuid" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" minOccurs="0" /> 
                                <xs:element name="AssetType" type="xs:string" minOccurs="0" /> 
                                <xs:element name="HostName" type="xs:string" minOccurs="0" /> 
                                <xs:element name="SerialNumber" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Manufacturer" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Model" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Status" type="xs:string" minOccurs="0" /> 
                                <xs:element name="SystemType" type="xs:string" minOccurs="0" /> 
                                <xs:element name="OSName" type="xs:string" minOccurs="0" /> 
                                <xs:element name="OSVersion" type="xs:string" minOccurs="0" /> 
                                <xs:element name="OSType" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Env" type="xs:string" minOccurs="0" /> 
                                <xs:element name="Details" type="xs:string" minOccurs="0" /> 
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
            </xs:complexType>
        </xs:element>
    </xs:schema>
</NewDataSet>

这是我在调试器中遇到的错误:

Unable to get property 'append' of undefined or null reference

这是触发它的jquery.jqGrdid.min.js中的一块:

if(d.p.gridview===!0&&(n=d.p.treeANode>-1?d.p.treeANode:0,d.p.grouping?g||(w.jqGrid("groupingRender",S,d.p.colModel.length,d.p.page,T),S=null):d.p.treeGrid===!0&&n>0?$(d.rows[n]).after(L.join("")):(Y.append(L.join("")),d.grid.cols=d.rows[0].cells)),d.p.subGrid===!0)

我尝试在我的ajaxGridOptions上设置超时,但似乎没有做任何事情,当我添加emptyrecords事件时也是如此。我也尝试使用loadError来管理错误,但它似乎没有抓住它。 我在beforeProcessing中尝试了很多东西,但我从未设法让它工作。

有没有人知道在崩溃我的jqGrid之前如何管理这个错误?

1 个答案:

答案 0 :(得分:0)

看起来这是jqGrid 5.0.0的一个问题,我恢复到4.5.4并且它开始完美运行。