【问题标题】:Polymer core-ajax xml response element is nullPolymer core-ajax xml 响应元素为空
【发布时间】:2015-01-10 05:45:13
【问题描述】:

我正在尝试使用 Polymer core-ajax 元素从在线 API 中提取数据。通过开发人员工具检查 API 请求是否成功,代码为 200,并返回预期的 XML 响应。但是响应对象为空,阻止我继续。

这是我的核心元素,

<polymer-element name="get-employees" attributes="employeeList">
    <template>      
        <core-ajax id="ajax" auto
               url="https://localhost:8443/GetEmployees"
               method='POST'
               headers='{"X-Token":"4517612052875154027"}'
               handleAs='xml'
               contentType="application/xml"
               response= "{{resp}}"
               on-core-complete="{{success}}"
               on-core-error="{{errorF}}"
               body='<?xml version="1.0" encoding="UTF-8" standalone="no"?><GetEmployees xmlns="http://www.22s.net/emp/1.0"><<Employees><Employee><EmployeeNo/><TitleCode/><Initials/>   <GivenNames/><Surname/></GetEmployees>'>
        </core-ajax>
        <template repeat="{{employee in resp | nodeList('Employee') }}" id="tem">
          <div class="empcard" flex>
            <div>{{employee || nodeText('GivenNames')}}</div>
          </div>
        </template>
    </template>
    <script>
        Polymer('get-employees', {
        errorF: function(){
            console.log('Error occured');
        },
        success: function(event, detail, sender){
            console.log(event);
        },          
        nodeList: function(element, name) {
            var arr;
            arr =  element ?
              [].slice.call(element.querySelectorAll(name)) : ['No Employees for the given query'];
            return arr;
        },
        nodeText: function(element, name) {
            return element.querySelector(name).innerHTML;

        }
    });
    </script>
</polymer-element>

返回的 XML 格式如下

<?xml version="1.0" encoding="UTF-8"?>
<GetEmployeesResponse xmlns="http://www.22s.net/emp/1.0">
<APIResponseStatus>
    <Code>OK</Code>
</APIResponseStatus>
<Employees>
    <Employee>
        <EmployeeNo>ISH001</EmployeeNo>
        <GivenNames>Omid</GivenNames>
        <Initials>OR</Initials>
        <Surname>KORDESTANI</Surname>
        <TitleCode>Mr</TitleCode>
    </Employee>
    <Employee>
        <EmployeeNo>ISH002</EmployeeNo>
        <GivenNames>Gary</GivenNames>
        <Initials>GR</Initials>
        <Surname>PARANTE</Surname>
        <TitleCode>Mr</TitleCode>
    </Employee>     
</Employees>
</GetEmployeesResponse>

当我在成功方法中调试 eventdetail 对象时,不携带返回的 XML 或 resp 对象。 nodeList 函数的 element 参数也返回“No Employees....”消息。

谁能告诉我我在哪里搞砸了。我尝试了其他方法,但到目前为止都没有成功。

编辑: 当我将响应 XML 保存在服务器文件中并向其发出 GET 请求时,页面显示,

[Object Element]  
[Object Element]
[Object Element]

通过模板。它完全相同的响应 XML。我糊涂了!!!!!!!!!

提前致谢, 伊什

【问题讨论】:

  • 看起来你可以 GET XML ok,所以我想知道你的服务器是否正确地从 POST 返回它。除了使用 core-ajax,你能写一个 vanilla XHR 看看你是否收到了来自 POST 的预期响应吗?

标签: javascript ajax xml polymer web-component


【解决方案1】:

我遇到了同样的问题。确保服务器返回正确的Header

'Content-Type: application/xml'

和/或返回文件的扩展名是.xml。我使用简单的 python SimpleHTTPServer 在同一个文件上对其进行了两次测试:该文件是否具有扩展名 .xml core-ajax 是否按预期工作;将扩展名更改为 .kml 导致 null 响应组件。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-03
    相关资源
    最近更新 更多