我正在使用Polymer 1.6和<iron-ajax>
进行API调用。我无法根据iron-ajax-error
事件来区分这两种情况:
403 Forbidden
)404 Not Found
)在这两种情况下,响应正文都是空的,即使在身份验证问题中,服务器也会使用JSON正文进行响应。
我想阅读响应状态代码,或者能够在情境1中获得响应主体。
答案 0 :(得分:7)
当服务器响应错误时,根据spec,响应正文应该是null
。但是,<iron-ajax>
的事件详细信息仍然可以访问状态代码和状态文本。
<iron-ajax>.response
事件 <iron-ajax>.response
事件的事件详情为<iron-request>
。状态代码存储在<iron-request>.status
中,相应的文本位于<iron-request>.statusText
:
_onResponse: function(e) {
const req = e.detail; // iron-request
console.log('status', req.status, req.statusText);
}
HTMLImports.whenReady(() => {
Polymer({
is: 'x-foo',
_onResponse: function(e) {
const req = e.detail;
console.log('response', req.status, req.statusText);
this.responseStatus = req.status;
this.responseStatusText = req.statusText;
},
_onError: function(e) {
const req = e.detail.request;
const err = e.detail.error;
console.log('error', err, req.status, req.statusText);
this.errorStatus = req.status;
this.errorStatusText = req.statusText;
this.errorMessage = err;
}
});
});
&#13;
<head>
<base href="https://polygit.org/polymer+1.11.1/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-ajax/iron-ajax.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<template>
<iron-ajax url="//httpbin.org/status/200"
auto
on-response="_onResponse"
on-error="_onError"></iron-ajax>
<div>'on-response' status: [[responseStatus]] ([[responseStatusText]])</div>
</template>
</dom-module>
</body>
&#13;
<iron-ajax>.error
事件请注意,<iron-ajax>.error
事件的详细信息与<iron-ajax>.response
的详细信息不同。 error
的事件详细信息是具有以下形状的对象:
{
error: String,
request: iron-request
}
因此<iron-ajax>.error
事件处理程序可以读取状态和状态文本,如下所示:
_onError: function(e) {
const req = e.detail.request; // iron-request
console.log('status', req.status, req.statusText);
}
HTMLImports.whenReady(() => {
Polymer({
is: 'x-foo',
_onResponse: function(e) {
const req = e.detail;
console.log('response', req.status, req.statusText);
this.responseStatus = req.status;
this.responseStatusText = req.statusText;
},
_onError: function(e) {
const req = e.detail.request;
const err = e.detail.error;
console.log('error', err, req.status, req.statusText);
this.errorStatus = req.status;
this.errorStatusText = req.statusText;
this.errorMessage = err;
}
});
});
&#13;
<head>
<base href="https://polygit.org/polymer+1.11.1/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-ajax/iron-ajax.html">
</head>
<body>
<x-foo></x-foo>
<dom-module id="x-foo">
<template>
<iron-ajax url="//httpbin.org/status/404"
auto
on-response="_onResponse"
on-error="_onError"></iron-ajax>
<div>'on-error' status: [[errorStatus]] ([[errorStatusText]]) - [[errorMessage]]</div>
</template>
</dom-module>
</body>
&#13;
答案 1 :(得分:0)
当我想查看错误案例的响应时,我将处理程序函数注册到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>