Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 本地laravel API上的Nativescript Vue axios调用错误[请求失败,状态代码为null]_Javascript_Android_Ios_Vue.js_Nativescript - Fatal编程技术网

Javascript 本地laravel API上的Nativescript Vue axios调用错误[请求失败,状态代码为null]

Javascript 本地laravel API上的Nativescript Vue axios调用错误[请求失败,状态代码为null],javascript,android,ios,vue.js,nativescript,Javascript,Android,Ios,Vue.js,Nativescript,我对Nativescript还比较陌生,在我的项目中,我试图向本地的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((r

我对Nativescript还比较陌生,在我的项目中,我试图向本地的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端点,但得到了响应,它可以工作,但我的LaravelAPI从未工作过

这确实有效:

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' }

知道我做错了什么吗?

由于您使用的是http,您需要将其添加到应用程序标记中的app/app\u Resources/Android/src/main/AndroidManifest.xml中: android:usesCleartextTraffic=“true”

像这样:

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

您的设备和pc是否在同一个wifi上?另外,端口80是默认的http,您不需要添加它。是的,它们位于同一个wi-Fi上,您可以尝试在端口80上公开(或完全忽略端口),然后只连接到ip地址吗?另外,首先尝试通过ping来访问您的pc,以排除与axios相关的问题:首先,您应该将get请求简化为非分页结果。只需返回['message'=>'success']。第二,为什么你在127上托管,但请求192?我使用宅地,它对我有用。但当我的笔记本电脑坏了,我不得不在生产服务器上测试。您也可以创建一个临时服务器并对其进行测试。@Trace我ping了我的IP,发送和接收了4个数据包,我尝试了ommiting端口,但得到了相同的错误