【问题标题】:Nativescript-Vue axios call error on local laravel API [Request failed with status code null]本地 laravel API 上的 Nativescript-Vue axios 调用错误 [请求失败,状态码为空]
【发布时间】:2020-03-25 10:49:44
【问题描述】:

我对 Nativescript 还很陌生,在我的项目中,我试图向我在 http://127.0.0.1:8000/ 上使用 php artisan serve 设置的本地 laravel 服务器发出 http 请求,我使用的是连接的 android 设备,没有模拟器。经过一番阅读,我使用 ipconfig 命令获得了本地 IPv4。

在我的 App.vue 组件中,我使用 axios 向本地 laravel API 端点发出 GET 请求:

这不起作用:

console.log('Firing axios call');
axios.get('http://192.168.1.35:8000/api/posts')
.then((response) => {
    console.log('It worked! /////////////////////////////////////////');
}).catch((err)=>{
    console.log('God dammit',err);
});

但我总是出错:

'God dammit' [Error: Request failed with status code null]

在 laravel 中,这是我响应 /api/posts 的控制器方法

public function list(Request $request)
    {
        $posts = Post::with(['postcategory','author'])
            ->latest()
            ->paginate($request->input('paginate', 10));

        return response()->json([
            'data' => $posts,
            'pagination' => [
                'total' => $posts->total(),
                'per_page' =>$posts->perPage(),
                'current_page' => $posts->currentPage(),
                'last_page' => $posts->lastPage(),
                'from' => $posts->firstItem(),
                'to' => $posts->lastItem()
            ]  
        ]);
    }

我尝试使用 httpbin 端点并得到响应,它可以工作,但我的 laravel API 永远无法工作...

这确实有效:

axios.get('https://httpbin.org/get')
.then((response) => {
   console.log('success');
   console.log(response.data);
}).catch((err)=>{
   console.log(err);
});

然后返回:

{ args: {},
JS:   headers:
JS:    { Accept: 'application/json, text/plain, */*',
JS:      'Accept-Encoding': 'gzip',
JS:      Host: 'httpbin.org',
JS:      'User-Agent': 'Dalvik/1.6.0 (Linux; U; Android 4.4.4; GT-I9060I Build/KTU84P)' },
JS:   origin: '79.152.179.88, 79.152.179.88',
JS:   url: 'https://httpbin.org/get' }

知道我做错了什么吗?

【问题讨论】:

  • 你的设备和电脑在同一个wifi上吗?另外,80端口是默认的http,不需要添加。
  • 是的,他们在同一个无线网络上
  • 您可以尝试在端口 :80 上公开(或完全省略端口)并连接到 IP 地址吗?也请先尝试通过 ping 排除与 axios 相关的问题来访问您的电脑:superuser.com/questions/586469/…
  • 首先,您应该将获取请求简化为非分页结果。只需返回 ['message' => 'success']。其次,为什么你在 127 上托管但请求到 192?我使用宅基地,它对我有用。但是当我有一台坏笔记本电脑时,我不得不在生产服务器上进行测试。您还可以制作一个登台服务器并对其进行测试。
  • @Trace 我ping了我的IP,发送和接收了4个数据包,我尝试省略端口,但我得到了同样的错误

标签: javascript android ios vue.js nativescript


【解决方案1】:

由于您使用的是 http,因此您需要将其添加到您的 app/App_Resources/Android/src/main/AndroidManifest.xml 中的应用程序标签中: android:usesCleartextTraffic="true"

像这样:

<application ...
    android:usesCleartextTraffic="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2020-03-29
    • 2018-05-03
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多