Javascript 聚合物铁ajax响应代码

Javascript 聚合物铁ajax响应代码,javascript,polymer,Javascript,Polymer,我正在使用Polymer 1.6和进行API调用。根据iron ajax错误事件,我无法区分这两种情况: 后端的基本身份验证失败(浏览器返回403禁止) 未找到服务(浏览器返回404未找到) 在这两种情况下,响应主体都是空的,即使在身份验证问题中,服务器使用JSON主体进行响应 我想读取响应状态代码,或者能够在情况1中获取响应主体。当我想查看错误案例的响应时,我向错误事件注册一个处理函数,并查找e.detail.response或e.detail.request.xhr.response,如下所

我正在使用Polymer 1.6和
进行API调用。根据
iron ajax错误
事件,我无法区分这两种情况:

  • 后端的基本身份验证失败(浏览器返回
    403禁止
  • 未找到服务(浏览器返回
    404未找到
  • 在这两种情况下,响应主体都是空的,即使在身份验证问题中,服务器使用JSON主体进行响应


    我想读取响应状态代码,或者能够在情况1中获取响应主体。

    当我想查看错误案例的响应时,我向
    错误
    事件注册一个处理函数,并查找
    e.detail.response
    e.detail.request.xhr.response
    ,如下所示:

    <dom-module id="my-login">
      <template>
        <iron-ajax url="http://some-url.com" 
               method="POST" 
               content-type="application/x-www-form-urlencoded" 
               body="{{reqBody}}" 
               handle-as="json"
               on-response="onResponse" 
               on-error="onError" id="xhr"></iron-ajax>
      </template>
    
      <script>
      Polymer({
          is: 'my-component',
          properties: {
            reqBody: {
              type: Object,
              value: {}
            }
          },
          onResponse: function(e) {
            console.log(e.detail.response);
          },
          onError: function(e) {
            // I think one of those two would be what you're looking for.
            console.log(e.detail.response);
            console.log(e.detail.request.xhr.response);
          }
      });
      </script>
    </dom-module>
    
    
    聚合物({
    是:'我的组件',
    特性:{
    请求主体:{
    类型:对象,
    值:{}
    }
    },
    onResponse:函数(e){
    控制台日志(如详细信息、响应);
    },
    onError:函数(e){
    //我想你要找的就是这两个人中的一个。
    控制台日志(如详细信息、响应);
    日志(e.detail.request.xhr.response);
    }
    });
    
    当我想查看错误案例的响应时,我将一个处理函数注册到
    error
    事件,并查找
    e.detail.response
    e.detail.request.xhr.response
    ,如下所示:

    <dom-module id="my-login">
      <template>
        <iron-ajax url="http://some-url.com" 
               method="POST" 
               content-type="application/x-www-form-urlencoded" 
               body="{{reqBody}}" 
               handle-as="json"
               on-response="onResponse" 
               on-error="onError" id="xhr"></iron-ajax>
      </template>
    
      <script>
      Polymer({
          is: 'my-component',
          properties: {
            reqBody: {
              type: Object,
              value: {}
            }
          },
          onResponse: function(e) {
            console.log(e.detail.response);
          },
          onError: function(e) {
            // I think one of those two would be what you're looking for.
            console.log(e.detail.response);
            console.log(e.detail.request.xhr.response);
          }
      });
      </script>
    </dom-module>
    
    
    聚合物({
    是:'我的组件',
    特性:{
    请求主体:{
    类型:对象,
    值:{}
    }
    },
    onResponse:函数(e){
    控制台日志(如详细信息、响应);
    },
    onError:函数(e){
    //我想你要找的就是这两个人中的一个。
    控制台日志(如详细信息、响应);
    日志(e.detail.request.xhr.response);
    }
    });
    
    当服务器响应错误时,响应主体应为
    null
    。但是,
    的事件详细信息仍然提供对状态代码和状态文本的访问

    .response
    事件 事件的事件详细信息是一个。状态代码存储在中,相应的文本存储在:

    \u on响应:函数(e){
    const req=e.detail;//铁请求
    控制台日志(“状态”、请求状态、请求状态文本);
    }
    
    HTMLImports.whenReady(()=>{
    聚合物({
    是‘x-foo’,
    _onResponse:函数(e){
    const req=e.detail;
    控制台日志(响应、请求状态、请求状态文本);
    this.responseStatus=请求状态;
    this.responseStatusText=req.statusText;
    },
    _onError:函数(e){
    const req=e.detail.request;
    const err=e.detail.error;
    控制台日志('error',err,req.status,req.statusText);
    this.errorStatus=请求状态;
    this.errorStatusText=req.statusText;
    this.errorMessage=err;
    }
    });
    });
    
    
    “响应时”状态:[[responseStatus]]([[responseStatusText]])
    
    当服务器响应错误时,响应主体应为
    null
    。但是,
    的事件详细信息仍然提供对状态代码和状态文本的访问

    .response
    事件 事件的事件详细信息是一个。状态代码存储在中,相应的文本存储在:

    \u on响应:函数(e){
    const req=e.detail;//铁请求
    控制台日志(“状态”、请求状态、请求状态文本);
    }
    
    HTMLImports.whenReady(()=>{
    聚合物({
    是‘x-foo’,
    _onResponse:函数(e){
    const req=e.detail;
    控制台日志(响应、请求状态、请求状态文本);
    this.responseStatus=请求状态;
    this.responseStatusText=req.statusText;
    },
    _onError:函数(e){
    const req=e.detail.request;
    const err=e.detail.error;
    控制台日志('error',err,req.status,req.statusText);
    this.errorStatus=请求状态;
    this.errorStatusText=req.statusText;
    this.errorMessage=err;
    }
    });
    });
    
    
    “响应时”状态:[[responseStatus]]([[responseStatusText]])