javascript - Axios 无法从 NativeScript 中的 API 获取数据
问题描述
我用 Laravel 创建了一个本地 API。我尝试使用 Postman 运行这个返回数据的 API,但是当我在 NativesScript Javascript 文件中使用 Axios 时,它既不返回数据也不返回错误消息。
我已经尝试在函数中使用 Async 和 Await ,但仍然没有返回任何内容。
import axios from 'axios'
export default class Service {
async testing(){
await axios.get('https://app.app/api/testing')
.then(res => {
console.log(res.data);
})
.catch(function(error) {
console.log(error);
});
}
}
我希望输出为 1,但它带有“”,我发现它永远不会进入 API(我已经放置了调试点,它没有触发器。),但邮递员工作得很好(相同的 url),它触发调试点,它确实返回值 1。
谢谢你。
解决方案
async/await 应该包含在 try/catch 块中。
try {
const result = await axios.get('https://app.app/api/testing');
console.log(result)
} catch(err) {
console.log(err);
}
推荐阅读
- postgresql - 出现异常:在使用 RollingUpdate 部署策略在 kubernetes 上部署服务时,“复制槽“XXX”对 PID XXX` 处于活动状态”
- jenkins - Jenkins升级后,构建失败时邮件无法显示环境变量
- c# - 我需要帮助理解链表算法 (C#)
- r - 如何从迷你图工具提示中的日期中删除逗号?
- angular-cli - 创建新的 Angular 项目时出现 npm 依赖错误
- javascript - 在 Rails 中,如何以 HTML 格式访问服务器端呈现的 React 组件
- html - 如何更改角度组件中的 :host{} 样式?
- reactjs - 大家好,一旦他们登录到网站,我如何在导航栏上显示用户名?
- hazelcast - Hazelcast Projection 获取单个字段
- swift - 如何为单选按钮着色?