我正在尝试使用axios来调用API并返回数据.
我有以下代码,工作正常
axios.get('http://api/courses')
.catch(function (error) {
if (error.response) {
console.log(error.response.status);
} else {
console.log('Error', error.message);
}
})
.then(response => {
console.log(response.data)
});
Run Code Online (Sandbox Code Playgroud)
这工作正常,API返回200和数据,所以在控制台我得到返回的数据.
但这不起作用
axios.get('http://api/courses')
.catch(function (error) {
if (error.response) {
console.log(error.response.status);
} else {
console.log('Error', error.message);
}
})
.then(response => {
console.log(response.data)
});
Run Code Online (Sandbox Code Playgroud)
在上面的调用中,我得到了从API返回的401,我希望能够检测到并执行某些操作(我当前正在执行console.log).但是在控制台中我收到以下错误:
GET http://api/courses 401 (Unauthorized)
(anonymous) @ spread.js:25
e.exports @ spread.js:25
e.exports @ spread.js:25
Promise.then (async)
r.request @ spread.js:25
r.(anonymous function) @ spread.js:25
(anonymous) @ index.js:20
(anonymous) @ (index):49
(index):58 Uncaught (in promise) TypeError: Cannot read …Run Code Online (Sandbox Code Playgroud) 我试图掌握 es6 类,但似乎无法将布尔值传递给构造函数。
在下面的代码中
export default class site_alert {
constructor(options) {
this.message = options.message || 'default cookie message';
this.autoDisplay = options.autoDisplay || true;
console.log(this.autoDisplay);
}
}
var sitemessage = new site_alert({
message:"Some string",
autoDisplay: false
});
Run Code Online (Sandbox Code Playgroud)
autoDisplayis always true,无论我创建类的实例时传递给它什么,它都采用默认值。
如果我改成autoDisplay一个字符串它工作正常。
你不能像这样将布尔值传递给构造函数吗?