How to call axios/api call in Nativescript application

后端 未结 2 1263
猫巷女王i
猫巷女王i 2021-02-04 16:37

I\'m trying to build a small application on Nativescript-vue where I\'m having backend laravel framework for api calls which needs to be called to get

相关标签:
2条回答
  • 2021-02-04 16:46

    I tested the exact same project you have shared in Github with Android 8.0, it works perfectly fine. The axios call hits the error block as the response status (error.response.status) is 401 and data (error.response.data) returns the exact same response you see from Postman.

    If you are using Android 9 or later it might fail as you haven't enabled useCleartextTraffic on your application tag in the AndroidManifest.xml.

    <application 
            android:usesCleartextTraffic="true"
            android:name="com.tns.NativeScriptApplication"
    

    With iOS also it would fail as you haven't enabled app transport security. Just to allow all Http communication within your app, you have to add the following key to your info.plist

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

    In case if you want to allow only specific domains, then use NSExceptionDomains key and list the endpoints.

    0 讨论(0)
  • 2021-02-04 16:48

    The problem has to do with how axios is imported. Try:

    import axios from 'axios/dist/axios'
    

    this also solves the Module not found: Error: Can't resolve 'net' in... error.

    When importing the package normally my requests failed, returning the error

    Error in v-on handler (Promise/async): "Error: Request failed with status code null"
    
    0 讨论(0)
提交回复
热议问题