【问题标题】:Using tns preview or tns preview --bundle axios on mobile doesn't work在移动设备上使用 tns preview 或 tns preview --bundle axios 不起作用
【发布时间】:2020-09-23 09:09:00
【问题描述】:

当我在 android 模拟器中运行时,我尝试将我的 api 与 nativescript 连接 axios 从后端获取所有数据,但是当我尝试使用 tns 预览或 tns 预览在实际移动设备上运行时 --bundle axios 不工作它给我空值

LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(Page)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(Page(3), ActionBar(4))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(ActionBar)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(gridlayout)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(Frame)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(stacklayout)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(textfield)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(StackLayout(6), Label(7))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(label)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(stacklayout)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(StackLayout(8), TextField(9))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(button)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(StackLayout(8), Button(10))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(StackLayout(6), StackLayout(8))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(GridLayout(5), StackLayout(6))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateComment()
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> CreateElement(ListView)
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(Page(3), GridLayout(5))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(ListView(11), Placeholder(13))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(GridLayout(5), ListView(11))
LOG from device Tarang shah: {NSVue (Vue: 2.6.11 | NSVue: 2.6.4)} -> AppendChild(Frame(2), Page(3))
**LOG from device Tarang shah: null**

任何人都知道如何解决这个问题 而我的后端 api 存储不在本地设备中

我的 axios 代码

axios({ method: "GET", "url": "http://api.sinhgadcollegeofscience.com/api/todos" }).then(result => {
                console.log(result.data)
                commit('setTodos', result.data)
            }, error => {       
                console.log('errorsssss')
            });

【问题讨论】:

    标签: axios nativescript-vue


    【解决方案1】:

    您正在发出一个明文请求 (http://...),我相信自 android API 28 以来默认禁用该请求。

    如果可以,请尝试使用https 连接。否则,您可以在AndroidManifest.xml 中启用明文流量

    <application
     <!-- ... -->
     android:usesCleartextTraffic="true"
     <!-- ... -->
    </application>
    

    进行更改后,删除平台文件夹,然后重新构建应用程序。

    更多详细信息可以在这里找到:Android 8: Cleartext HTTP traffic not permitted

    【讨论】:

    • 感谢您的重播,我尝试了,但它在移动设备上不起作用(有安卓 10),当我尝试调试 tns 时它会发送空值,但在实际设备上无法正常工作
    • 仔细检查您是否可以从手机访问该网址 - 例如尝试在实际设备上的浏览器中打开它,看看它是否可以连接。
    猜你喜欢
    • 2021-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-27
    • 2020-11-15
    • 1970-01-01
    相关资源
    最近更新 更多