使用本地API开发Nativescript应用 [英] Developing Nativescript app with Local API

查看:70
本文介绍了使用本地API开发Nativescript应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,我对NativeScript还是很陌生.我有一个示例项目.尝试对 local API进行简单的get请求.这是我的本地开发机器上的网站设置,用于:

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

我正在尝试访问本地API上的终结点,但似乎未返回任何内容.但是我可以将API端点更改为不是本地API的东西,有时它返回东西.

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.

API是Laravel/PHP api,我可以确认它实际上是在返回有效的json,所以我认为API并不是问题.这是我的文件.

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.

推荐答案

所以我找到了问题,以为我会发布.问题不是我的端点或NS安装.这是因为,如果不对模拟器上的主机设置进行一些修改,即开即用的Android模拟器无法引用在您的开发机上运行的本地站点.在此说明了此过程,但我想向遇到相同问题的任何人强调.

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模拟器不支持开箱即用访问本地计算机站点,如下所述:它需要通过设备 Everytime 进行设置,除非有人有解决方案只能将其设置一次,否则我希望看到它在这里共享.

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.

有些服务器解决方案概述了类似的步骤,但是我在每一个解决方案上都遇到了问题.这是概述问题要点的一个: http://sadhanasiblog.blogspot.in/2012/07/local-environment-setup-for-android.html

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

感谢您的帮助.

这篇关于使用本地API开发Nativescript应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆