Developing Nativescript app with Local API

爷,独闯天下 提交于 2020-01-17 04:43:10

问题


This may be a silly question and I am very new to NativeScript. I have a sample project up. Trying to do a simple get request to a local api. This is a website setup on my local dev machine for:

http://test-api.dev/api/v1

I am trying to access an endpoint on my local API and it does not seem to return ANYTHING. But I can change the API endpoint to something that is not a local API and sometimes it return stuff.

The API is a Laravel/PHP api and I can confirm it is infact returning valid json, so I don't think the API is the problem. Here is my file.

var config = require("../../shared/config");
var fetchModule = require("fetch");
//var http = require("http");
var ObservableArray = require("data/observable-array").ObservableArray;

function GroceryListViewModel(items) {
    var viewModel = new ObservableArray(items);
    viewModel.load = function() {
        // http.getJSON(config.apiUrl + "events").then(function(result) {
        //  console.log(JSON.stringify(result));
        // }, function(error) {
        //  console.error(JSON.stringify(error));
        // });

        return fetch(config.apiUrl + "events", {
                headers: {
                    //"Authorization": "Bearer " + config.token
                    "Content-Type": "application/json"
                }
            })
            .then(handleErrors)
            .then(function(response) {
                console.log(response);
                return response.json();
            }).then(function(data) {
                console.dump(data);
                data.forEach(function(event) {
                    viewModel.push({
                        name: event.name,
                        id: event.id
                    });
                });
            });
    };

    viewModel.empty = function() {

        while (viewModel.length) {
            viewModel.pop();
        }
    };
    return viewModel;
}

function handleErrors(response) {
    if (!response.ok) {
        console.log(JSON.stringify(response));
        throw Error(response.statusText);
    }
    return response;
}

module.exports = GroceryListViewModel;

Please go easy on me, definitely seeking help on this though. Thank you all in advance.


回答1:


return fetch(config.apiUrl + "events", { - so the fetch request is expecting the first argument to be a URL. So where config.apiUrl is defined is where the request goes. You can change this to point to the localhost instance of your server you are running.




回答2:


So I have found the issue, thought I would post it. The problem wasn't my endpoints or my NS install. It is because Android Emulators out of the box cannot reference local sites running on your dev machine without some modification to the hosts settings on the Emulator. This process is explained here but I want to emphasize for anyone having the same issue.

Android emulators do not support access to local machine sites out of the box as explained here: https://developer.android.com/studio/run/emulator-networking.html

It needs to be setup through the device EVERYTIME unless someone has a solution to set this up only once, I would love to see it shared here.

There are serveral solutions outlining similar steps but I have had issues on every single one. Here is one that outlines the gist of the issue: http://sadhanasiblog.blogspot.in/2012/07/local-environment-setup-for-android.html

Thanks for the help.



来源:https://stackoverflow.com/questions/43150805/developing-nativescript-app-with-local-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!