Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

javascript - 本地laravel API上的Nativescript-Vue axios调用错误[请求失败,状态码为空](Nativescript-Vue axios call error on local laravel API [Request failed with status code null])

I'm fairly new to Nativescript, in my project I'm trying to make an http request to my local laravel server I set up with php artisan serve on http://127.0.0.1:8000/ , I'm using a connected android device, no emulator.

(我对Nativescript相当陌生,在我的项目中,我试图向使用php artisan在http://127.0.0.1:8000/上设置的本地laravel服务器发出http请求,我正在使用连接android设备,没有模拟器。)

After some reading I got my local IPv4 with ipconfig command.

(经过一番阅读后,我使用ipconfig命令获取了本地IPv4。)

In my App.vue component I fire a GET request to my local laravel API endpoint with axios:

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

This DOESN'T work:

(这不起作用:)

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);
});

But I always get error:

(但是我总是会出错:)

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

In laravel, this is my controller methos which responds to /api/posts

(在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()
            ]  
        ]);
    }

I tried with httpbin endpoint and I get response back, it works but my laravel API never works...

(我尝试使用httpbin端点,但得到响应,但是我的laravel API无法正常工作...)

This DOES work:

(这样做的工作:)

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

And returns:

(并返回:)

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

Any idea what I'm doing wrong?

(知道我在做什么错吗?)

  ask by gabogabans translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...