I am a newcomer to Nativescript, and in my current project, I am attempting to send an http request to my locally hosted Laravel server using php artisan serve on . I am utilizing a physical android device for testing rather than an emulator. After doing some research, I found my local IPv4 address using the ipconfig command.
Within my App.vue component, I trigger a GET request to my Laravel API endpoint with axios:
This code snippet does not produce the desired result:
console.log('Initiating axios call');
axios.get('http://192.168.1.35:8000/api/posts')
.then((response) => {
console.log('Request successful! /////////////////////////////////////////');
}).catch((err)=>{
console.log('Error occurred',err);
});
Unfortunately, I constantly receive this error message:
'Error occurred' [Error: Request failed with status code null]
In my Laravel backend, this is the controller method that corresponds to /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()
]
]);
}
When I tested it with the httpbin endpoint, I received a response successfully, but my Laravel API remains unresponsive...
On the other hand, this code snippet works as expected:
axios.get('https://httpbin.org/get')
.then((response) => {
console.log('Success');
console.log(response.data);
}).catch((err)=>{
console.log(err);
});
It returns the following data:
{ 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' }
Do you have any insights into what might be causing the issue?