- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试在 Nativescript-vue
上构建一个小型应用程序,其中我有用于 api 调用的后端 laravel
框架,需要调用它来获取相关数据.例如,如果用户想要登录,他需要通过 api oauth/token
验证其凭据,所以我试图通过 axios
调用它,这是我的代码:
我的 settings.js
文件包含。
export const authHeader = {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
这是在我的 axios 调用中导入的:
const postData = {
grant_type: 'password',
username: user.email,
password: user.password,
client_id: clientId,
client_secret: clientSecret,
scope: '',
provider: provider
}
const authUser = {}
axios.post(authUrl, postData, {headers: authHeader}).then(response => {
console.log('Inside oauth/token url')
if(response.status === 200)
{
console.log('response received')
}
})
.catch((err) => {
if(err.response.status === 401){
reject('Validation error')
}
else
reject('Something went wrong')
})
当我尝试使用命令 tns debug android --bundle
构建时,我得到了 chrome-devtools
,它向我显示:
更深入地研究它,我可以看到正在传递 header ,但这些只是临时的:
如您所见,我的应用程序中有 console.log
,它向我显示:
即使在编译时我也会遇到以下错误:
指导我如何实现这一目标。谢谢。
编辑:
同样我使用了nativescript的
自己的http
documentation像这样:
const httpModule = require("http");
httpModule.request({
url: "http://iguru.local/oauth/token",
method: "POST",
headers: { "Content-Type": "application/json" },
content: JSON.stringify({
grant_type: 'password',
username: this.user.email,
password: this.user.password,
client_id: 'check',
client_secret: 'check',
scope: '',
provider: 'check'
})
}).then((response) => {
// Argument (response) is HttpResponse
console.log('Action called')
if(response.status === 200)
{
console.log('Response recieved')
}
}, (e) => {
console.log('Something went wrong')
});
我得到了相同的结果,而且我尝试了来自服务器端 ex http://confidenceeducare.com/oauth/token 的 api它恰好是一样的。正常的 Vue
应用程序调用 api 非常好。我猜 nativescript
应用程序有问题。我需要导入更多东西吗?我坚持下去。
如果有人认为我的 api 端点已损坏,我尝试使用示例中提到的 url,即 https://httpbin.org/post
:
和:
当我在 postman
中检查我的 api 时,它在那里工作,我至少得到了一个带有状态代码的响应:
编辑 2:供引用 github 存储库 https://github.com/nitish1986/sample_mobile_app
最佳答案
我使用 Android 8.0 测试了您在 Github 中共享的完全相同的项目,它运行良好。 axios 调用命中错误 block ,因为响应状态 (error.response.status
) 是 401 并且数据 (error.response.data
) 返回与您看到的完全相同的响应来自 postman 。
如果您使用的是 Android 9 或更高版本,它可能会失败,因为您没有在 AndroidManifest.xml
中的 application
标签上启用 useCleartextTraffic
>.
<application
android:usesCleartextTraffic="true"
android:name="com.tns.NativeScriptApplication"
对于 iOS,它也会失败,因为您没有启用应用程序传输安全性。为了允许您的应用程序内的所有 Http 通信,您必须将以下键添加到您的 info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
如果您只想允许特定域,则使用 NSExceptionDomains
键并列出端点。
关于javascript - 如何在 Nativescript 应用程序中调用 axios/api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55184243/
如何在 Nativescript + Angular 中实现 RadioButtons? 我试过 RadioButton 插件,但它在编译时出错。我在 stackoverflow 上看到一篇文章说要使
每当我单击 window 的 nativeScript sidekick 下载链接时,它都会将我重定向到主页。我在那里没有看到 nativescript 助手的任何下载链接。所以我寻找关于sideki
如何在 nativescript-angular 中的分段栏中添加图像图标。你能帮我解决这个问题吗
我正在寻找一个开源的 NS7 替代 nativescript 下拉菜单。有没有人有建议。我正在将我的应用程序移植到 NS7,但无法找到替代付费版本插件的插件。 最佳答案 我的解决方法是打开一个包含下拉
enter image description here如何在 android 中更改启动画面 (LaunchScreenTheme) 的状态栏图标颜色? 我尝试了下面的代码,但它不起作用。 #FFF
Nativescript 5 已发布,我正在尝试更新 tns 版本。目前它 tns --version 显示为 4.3.2。 我遵循了 https://docs.nativescript.org/re
我想将大小为 3000*4200 像素的 SD 卡中的图像调整为 250*250 大小的个人资料图片。我尝试了很多使用 android BitmapFactory 的方法,但没有任何效果。 有这个插件
我想在 NativeScript 的绝对布局中在屏幕底部放置一个元素。 我有这个代码: 我发现 Absolut
最近我在用 nativescript 编写一个应用程序,现在我遇到一个问题,我不知道如何保存用户的登录状态。比如用户第一次登录,以后就不需要登录了。那么我该如何实现呢? 最佳答案 我确实在我的应用程序
我正在尝试在我的 NativeScript 应用程序中创建具有动态行数和列数的表或数据网格。我有一些产品类别和这些类别中的一些产品。产品对其所属类别有一些规范。以下是图形卡类别产品的 JSON 示例:
我没能找到列表选择器的 NS+JS 工作示例,文档中的示例都没有 XML 示例。有人可以帮忙吗? 最佳答案 您需要做的是绑定(bind)一个 的 items属性到页面上的数组 bindingConte
我正在尝试更改 nativescript 中 progress 组件的高度,如下所示: 但是 height 属性没有效果。如何更改此组件的高度? 最佳答案 您可以使用 height 属性更改 Pr
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 2年前关闭。 Improve this qu
在 NativeScript 中,是否可以从 Nativescript 应用程序中启动另一个应用程序? 假设我在一个应用程序中有一个按钮,当按下它时,它会启动 Waze 导航应用程序,并带有一个位置的
你如何附加到文件而不是覆盖它? file-system module文档解释了如何写入文件: The following example writes some text to a file. It
对于NS项目,如何设置最低iOS版本。 我有IPHONEOS_DEPLOYMENT_TARGET = 8.0;在我的build.xcconfig文件中 但是在构建过程中,我收到未设置的消息。 在构建过
我想从 NativeScript 项目中的自定义组件公开一个方法。这是概念设置: 我的组件.xml ...UI markup... MyComponent.ts export function l
使用最新版本的 Nativescript,我根据文档创建了一个插件,并且在运行 tns plugin add ../nativescript-keychain 之后我收到消息 Successfully
NativeScript 中 DIV 的替代方法是什么? 如何使用内嵌内容的 block ? 最佳答案 在浏览器中,div 的行为由 display CSS 属性决定。 例如,默认为block和ver
目前有以下命令: tns cloud build android --accountId= 返回这一行:Invalid accountId index provided 您如何为您的帐户获得正确的 a
我是一名优秀的程序员,十分优秀!